On 09/10/12 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?


This looks a bit like it might be homework so I won't answer directly but will give some general comments that might help...

  another='y'
     while another=='y' or another=='Y':

This line should not be indented.
It's usually easier to convert the string being tested to
a known case. Either

while another.lower() == 'y':
or
while another.upper() == 'Y':

         choice()

choice does not return any values so you cannot access the
data inside. You want to get back the cost (and maybe the
book too!)

         another=input('Do you have another book to order for this
student? '+\
                       'Enter y for yes: ')

Using a line continuation character is not needed here.
Using string addition is usually the wrong approach in Python. In this case you could use string literal concatenation:

another=input('Do you have another book to order for this student?',
              'Enter y for yes: ')

or just a single long line. In case you think this will put it on two lines you are wrong, it will need an exp[licit newline character ('\n') to do that:

another=input('Do you have another book to order for this student?',
              '\nEnter y for yes: ')

or you could use triple quotes:

another=input('''
              Do you have another book to order for this student?
              Enter y for yes: ''')

Lots of choices...

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

while this would work I'd prefer to use an else clause to catch unexpected values explicitly rather than rely on the initialisation:

      cost=0   # this becomes redundant
      if book==1:
          cost=79
      elif book==2:
          cost=49
      elif book==3:
          cost=19
      else:
           cost = 0

     b_total=float(cost*num)
     print('Your book cost is $ ',format(b_total,'.2f'))

As a general rule its better to keep presentation and logic separate so rather than printing the result inside the function return the value and the calling code can then print it out. Or store it for further processing(hint!)

def choice():
    # ... as above except the end: ...
    b_total=float(cost*num)
    return b_total

And where you call it

print('Your book cost is $ ',format(choice(),'.2f'))

To do more than one book you need your loop to collect the values rather than print them (or add them as you go) then when done sum()
the values and print the result.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to