+ 2

How to enter only 4 digit no. in python

if you will enter more and less it will show error

25th Aug 2018, 2:27 PM
Rishabh Mehta
3 Respuestas
+ 5
After getting input as int n > 9999 or n < 1000 :x or maybe just len(str(n)) != 4
25th Aug 2018, 2:31 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
You could also do it in one compare: if 1000 <= x < 10000: # do stuff
29th Aug 2018, 9:03 PM
Zuke
Zuke - avatar
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
25th Aug 2018, 8:06 PM
Just A Rather Ridiculously Long Username