ValueError: could not convert string to float — user input with Python -
i'm trying write 'while' loop takes users input, if number remembers it, if blank space breaks. @ end should print average of entered numbers. giving me error 'could not convert string float: '. wrong here? thanks!
edit: re-wrote , same error converting, seems on final (count += 1) line?
number = 0.0 count = 0 while true: user_number = input('enter number: ') if user_number == ' ': break print (number / count) number = number + float(user_number) count += 1
my guess directly hit enter when don't want pass numbers anymore. in case, comparing space incorrect.
number = 0.0 count = 0 while true: user_number = input('enter number: ') if user_number == '': break number += float(user_number) count += 1 print (number / count)
also statement after break
unreachable.
if want cleaner alternative, recommend appending list, , computing average. removes need separate counter. try this:
numbers = [] while true: user_number = input('enter number: ') if user_number == '': break numbers.append(float(user_number)) print (sum(numbers) / len(numbers))
additionally, remove need break
testing in head of while
, you'll need take additional input outside loop.
Comments
Post a Comment