+ 2
How to enter only 4 digit no. in python
if you will enter more and less it will show error
3 Respostas
+ 5
After getting input as int
n > 9999 or n < 1000
:x
or maybe just
len(str(n)) != 4
+ 1
You could also do it in one compare:
if 1000 <= x < 10000:
# do stuff
0
Here's one way to do it
while True:
try:
n = int(input('4-digit number: '))
if n in range(1000, 10000):
break
except:
pass