How to enter only 4 digit no. in python | Sololearn: Learn to code for FREE!

+3

How to enter only 4 digit no. in python

if you will enter more and less it will show error

8/25/2018 2:27:29 PM

Rishabh Mehta

3 Answers

New Answer

+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