Click here to Skip to main content
13,865,661 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')
        val_input = float(user_input)
    except ValueError:
        print('You have typed in an invalid value. Please type in a numerical whole number')
Updated 13-Jul-18 22:12pm

1 solution

Rate this: bad
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.

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
Web02 | 2.8.190214.1 | Last Updated 14 Jul 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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