0

I'm writing a basic program which is to create a team based on age groups. So I want to create a team, but for example for the first team they have to be between the age of 20 and 25:

while True:
        if(num<20):
            num = int(input("Too young, try again!: "))
        elif(num>25):
            num = int(input("Too old, try again!: "))

I also want to display a message in the if statement if the user enters text rather than an integer, for example:

elif(num is not int):
    num = int(input("Please enter integers(numbers)only!: ")

I'm new to Python so sorry if the example is poor.

Yurty123
  • 91
  • 6
  • 1
    Does this answer your question? [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – theknightD2 Apr 21 '21 at 16:46

2 Answers2

2

You're looking for try/except Exception Handling

try:
    value = int(input(...))
except ValueError:
    # handle Exception

this works because int() raises ValueError on non-int inputs

>>> int("not an integer")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'not an integer'
>>> int("5")
5
ti7
  • 16,375
  • 6
  • 40
  • 68
0

would recomend try or except

try:
   value = int(input(...))
execpt:
  print("intiger please")