Click here to Skip to main content
13,632,131 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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 13-Jul-18 17:52pm
Updated 13-Jul-18 21:12pm
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.180712.1 | Last Updated 14 Jul 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100