Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to programming. I am trying to write a while loop that takes in user-inputted numerical data. The loop breaks when the user input "done". I am trying to figure out a way to store and print all the valid numerical data of the user.

What I have tried:

Python
values = []
while True:
    user_input = input('Please type in a numerical value')
    if user_input == 'done':
        print('All Done')
        break
    try:
        val_input = float(user_input)
    except ValueError:
        print('You have typed in an invalid value. Please type in a numerical whole number')
        continue
    values.append(user_input)
print(val_input)
Posted
Updated 13-Jul-18 21:12pm
v2

1 solution

You are converting user_input to a floating point number, but you want the user to enter a whole number. Using int type may be a better choice. Secondly, you are appending the user_input value to your list, rather than the converted number. And lastly, you are printing the last converted value_input, instead of the items in your values list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900