Reputation: 143
print('Input a list of numbers to do statistics on them. Type stop to end the list.')
while True:
number_list = []
stop_input = False
while stop_input == False:
user_input = input('-> ')
if float(user_input):
number_list.append(user_input)
elif user_input == 'stop':
stop_input = True
print('Sum:', sum(number_list))
The error is as follows:
if float(user_input):
ValueError: could not convert string to float: 'stop' (line 10).
I am typing input in as
1.0
2.0
3.0
stop
Upvotes: 0
Views: 526
Reputation: 18940
float('stop')
will raise an exception that you have to catch with a try: ... except ...: ...
block.
Additionally, float(user_input)
evaluates to False
when user_input
is 0.0
(or 0
), so that number would never be added to the list.
You can change:
if float(user_input):
number_list.append(user_input)
elif user_input == 'stop':
stop_input = True
to:
try:
number_list.append(float(user_input))
except ValueError:
if user_input == 'stop':
stop_input = True
to fix your program.
Upvotes: 2