How do I make this question asking for an input over and over until the user gets a valid answer between 5 and 25?
newGen = int(input("Input number of new generations to model (should be between 5 and 25\n)"))
How do I make this question asking for an input over and over until the user gets a valid answer between 5 and 25?
newGen = int(input("Input number of new generations to model (should be between 5 and 25\n)"))
newGen = 0
while newGen not in range(5, 26):
newGen = int(input("Input number of new generations to model (should be between 5 and 25\n)"))
You need to catch the ValueError and loop it.
newGen = None
while newGen is None or (newGen < 5 or newGen > 25):
try:
newGen = int(input("Input number of new generations to model (should be between 5 and 25\n)"))
except ValueError:
pass