-2

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)"))
Max Cenci
  • 3
  • 1

2 Answers2

0
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)"))
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

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
nephlm
  • 543
  • 3
  • 11