How do you check if input is numerical in Python?

Titchy2005

I have written a script in Python and I want to make it so if you don't input a number when it asks "What's your age?" It will say "Please input a number" and then repeat the original question. I have tried many different suggestions I have seen on other posts but none of them are working.

Here is the script:

yesorno = "yes"

while yesorno == "yes" :
    import time
    Age = 0
    Age = int(input("What is your age?"))
    Age = Age*365*24
    print(Age)
    yesorno = input("Would you like to run again?(yes/no)")

while yesorno != "no" :    
print("Please input yes or no")
yesorno = input("Would you like to run again?(yes/no)")

while yesorno == "no" :
print("Program stopping...")
import sys
sys.exit("Program stopping...")
Davy M

There are many ways to do this, but allow me to recommend one that adds to your existing code, focusting on this line:

Age = int(input("What is your age?"))

As you probably already know, if the person inputs something that isn't an integer, this line throws an error.

You can catch these errors in a try...except... block, excepting the error that's thrown when any non-int is inputted in that line.

try:
    Age = int(input("What is your age?"))
except ValueError:
    print("Please input a number")

To make this continue the loop, I'd put the yesorno in the try statement so it only executes if the Age input was successful, otherwise, react as if the user inputted yes, as so:

 try:
    Age = int(input("What is your age?"))
    Age = Age*365*24
    print(Age)
    yesorno = input("Would you like to run again?(yes/no)")
except ValueError:
    print("Please input a number")
    yesorno = "yes"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: How do you sort the contents of a .txt file in numerical order?

From Dev

How do I gather user numerical input into a list in Python?

From Dev

How do you read input() as a string in python

From Dev

how do you check an input in java is a certain word or character

From Dev

How do you check if an input is a negative number in VB

From Dev

How do you check to see if an input is equal to a certain integer or integers?

From Dev

How do I check if input is a fraction (Python)?

From Dev

How do I check if input is a number in Python?

From Dev

How do I check an input for an integer in Python?

From Dev

How do I check if input is a number in Python?

From Dev

How do you check whether you're in a transaction with ZODB in Python?

From Dev

How do you check whether you're in a transaction with ZODB in Python?

From Dev

Python how can you check for consecutive letters in a user input?

From Dev

Numerical Python - how do I make this a ufunc?

From Dev

How do you get the numerical value from a string of digits?

From Java

How do you check in python whether a string contains only numbers?

From Dev

How do you check in which module a function is defined? Python

From Dev

How do you check in which module a function is defined? Python

From Dev

How do you check that a dictionary is in an array even in the worng order in python?

From Dev

How do you check the class of a Python csv.writer instance?

From Dev

How do you quantize a simple input using python

From Dev

How do I check if raw input is integer in python 2.7?

From Dev

How do I check input variable validity from a Python extension?

From Dev

How do I check an input answer in python 3

From Dev

Check for only numerical input in batch file

From Dev

How to check user input (Python)

From Dev

How to check user input (Python)

From Dev

How do I update a Label with a numerical value with python?

From Dev

How do you check if a struct is initialized in C?

Related Related

  1. 1

    Python: How do you sort the contents of a .txt file in numerical order?

  2. 2

    How do I gather user numerical input into a list in Python?

  3. 3

    How do you read input() as a string in python

  4. 4

    how do you check an input in java is a certain word or character

  5. 5

    How do you check if an input is a negative number in VB

  6. 6

    How do you check to see if an input is equal to a certain integer or integers?

  7. 7

    How do I check if input is a fraction (Python)?

  8. 8

    How do I check if input is a number in Python?

  9. 9

    How do I check an input for an integer in Python?

  10. 10

    How do I check if input is a number in Python?

  11. 11

    How do you check whether you're in a transaction with ZODB in Python?

  12. 12

    How do you check whether you're in a transaction with ZODB in Python?

  13. 13

    Python how can you check for consecutive letters in a user input?

  14. 14

    Numerical Python - how do I make this a ufunc?

  15. 15

    How do you get the numerical value from a string of digits?

  16. 16

    How do you check in python whether a string contains only numbers?

  17. 17

    How do you check in which module a function is defined? Python

  18. 18

    How do you check in which module a function is defined? Python

  19. 19

    How do you check that a dictionary is in an array even in the worng order in python?

  20. 20

    How do you check the class of a Python csv.writer instance?

  21. 21

    How do you quantize a simple input using python

  22. 22

    How do I check if raw input is integer in python 2.7?

  23. 23

    How do I check input variable validity from a Python extension?

  24. 24

    How do I check an input answer in python 3

  25. 25

    Check for only numerical input in batch file

  26. 26

    How to check user input (Python)

  27. 27

    How to check user input (Python)

  28. 28

    How do I update a Label with a numerical value with python?

  29. 29

    How do you check if a struct is initialized in C?

HotTag

Archive