ohhi_hannah ohhi_hannah - 4 years ago 125
Python Question

Break a for loop that is prompting input to list?

I'm trying to make a simple program that accepts input of 'sales amounts' individually for each day of the week then totals the values and displays them.

I have a loop that accepts 7 inputs, but I would like the user to be able to enter

'q'
to break the loop if they have less than 7 inputs.
Here's what I have:

sales = []
for i in range(0, 7):
sales.append(input("> "))
if 'q':
break


It is giving me a
NameError
, but I've tried a variety of things.

I've done
if sales/input/raw_input == "q":
.

I've also set
q
equal to a variable, but that terminated my loop after one iteration.

Answer Source

store the input in a variable so you can reuse it without requiring the user to re-enter the value

user_input = input("> ")
if user_input == "q":
    break
sales.append(user_input)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download