# [Tutor] using the loop function while another

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Oct 10 00:24:59 CEST 2012

```Mark Lawrence wrote:
> On 09/10/2012 20:47, Amanda Colley wrote:
> > Ok, here is my problem.  If a person has two  different people to order
> > books for, then when using the loop option, How do you add the two seperate
> > choices together for a  final total of books cost?
> >
> >   another='y'
> >      while another=='y' or another=='Y':
> >          choice()
> >          another=input('Do you have another book to order for this student?
> > '+\
> >                        'Enter y for yes: ')
> >
> > def choice():
> >      book=int(input('Enter the book chioce you want: ' +\
> >                 'For Hardback Enter the number 1: ' +\
> >                 'Paperback Enter the number 2: ' +\
> >                 'Electronic Enter the number 3: '))
> >      num=int(input('Enter how many you need: '))
> >      cost=0
> >      if book==1:
> >          cost=79
> >      elif book==2:
> >          cost=49
> >      elif book==3:
> >          cost=19
> >      b_total=float(cost*num)
> >      print('Your book cost is \$ ',format(b_total,'.2f'))
> >
> >
>
> Create an empty list at the top of your loop.  Store your b_total in the
> list inside the loop with the append method.  When the loop finishes use
> the built-in sum function to get the final total.
>
> --
> Cheers.
>
> Mark Lawrence.
>

I would also have choice() return the "choice" and "num" and then let the
function calling it assign the book price.

another='y'
while another=='y' or another=='Y':
book_format = choice() # rename to be more descriptive e.g. get_book_format()
price = price_by_format(book_format)
subtotal.append(price)
another=input('Do you have another book to order for this student? '+\
'Enter y for yes: ')

The backslash near the + is also unnecessary and I consider it a
sign of a possible bad habit/practice. You can usually wrap your multi-line
expressions with a parenthesis.

input(' blah ' +
'blah2')

OR

x = (something *
something_else + some_fucntion_call() +
last_thing_here)

OR

if blah and ( blah != shoo and
shoo != 1 and shoo < 5 and blah is not None ):
print('true')

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
```

More information about the Tutor mailing list