0

I cant seem to get the total from my while true loop. I am trying to write a code . That will get the total value for all the seats a waiter enters. But I keep getting the total like this Total:$ q I had set q to break if the user input it for a seats. Can you help me please. Here is my syntax.

while True:                                           
    seats = raw_input("Enter the value of the seat [q to quit]:")
    if seats  ==  'q':
       break
    print "Total: $", seats
Marcos
  • 4,643
  • 7
  • 33
  • 60
Matias
  • 15
  • 1
  • 5

2 Answers2

0
total = 0
while True:
    seats = raw_input("Enter the value of the seat [q to quit]:")
    if seats == 'q':
        break
    total += int(seats)
print "Total: $", str(total)

If you want it to show the total after each loop just push the print statement inside the loop.

Wright
  • 3,370
  • 10
  • 27
  • Hello Wright thanks for replying to my question. Well I tried your method but when i try to enter q to quit if gives me an error message. Does it matter that im using python 2.7.6 – Matias Mar 16 '17 at 00:49
  • 1
    Wow it worked Wright I appreciate you and your intellect. I just went ahead and changed the int(seats) to float(seats) so it can work for any input that has a decimal in it. Thanks a lot Wright : ) – Matias Mar 16 '17 at 01:15
  • Ah that's great news :) – Wright Mar 16 '17 at 01:18
0

You need to sum variable seats with your input value, not reassigning value. You were wrong in choosing operator, you need to use +=, not =.

Replace your code with this:

 while True:
     input = raw_input("Enter the value of the seat [q to quit]:")
     if input  ==  'q':
         break
     seats += input
 print "Total: $", seats

After that, read this question and answers: What exactly does += do in python?

Community
  • 1
  • 1
SyncroIT
  • 1,510
  • 1
  • 14
  • 26
  • I appreciate you anwsering Syncro but its not working now it wont run the program im using python 2.7.6 does that matter – Matias Mar 16 '17 at 00:43
  • I appreciate it Syncro but Wright figured it out I wasn't setting a variable named total so that i can keep the total amount in a variable. And it can be printed out as total – Matias Mar 16 '17 at 01:31