I want restrict the input to a range of numbers (say n).
If I use while x not in range(n): x = input()
, it is not working.
So i put while x not in range(n): x = int(input())
.
It works fine unless I give any letters.
After some research I came up with the following code:
x = None
while True:
try:
while x not in range(n+1):
x = int(input("X (1 to "+ str(n)+ ") :"))
if x not in range(n+1):
print("please enter a valid input")
break
except:
print("please enter a valid input")
I want to know if there is any way to shorten the code like merging both the while loops and
or or
.