+ 1
Please help me to solve this problem in this problem how to check user entered number is 4 digit or not?
import random x= random.randint(1000,9999) num=input() print("system generated number is ",x) print("The number you entered is ",num) if num == x: print("Congrats you win") else: rabbit=0 tortose=0 unum = str(num) snum = str(x) c1=0 for i in unum: c1=c1+1 c2 = 0 for j in snum: c2=c2+1 if(i==j): if(c1==c2): rabbit=1 else: tortose=1 if rabbit==1: print("you got rabbit") elif rabbit==0 and tortose==1: print("you got tortose") else: print("Bad Luck you dont have any match")
5 odpowiedzi
+ 6
Mufeeda ,
a number can also be checked with range() function if it fits a condition:
sample:
...
if (num := int(input())) in range(1000, 9999 +1): # this line takes input, converts it to int, assigns it to variable and check if it is in range...
print(f"{num} is in range") # or do whatever...
else:
print(f"{num} out of range") # or do whatever...
...
+ 4
“if num >= 1000 and num <= 9999”?
+ 3
Would you prefer if it's coded this way
https://code.sololearn.com/clkaEK4BWSTX/?ref=app
+ 2
Or you could check the length of the input string
0
#use this:
def is_4_digit(num):
return len(str(num)) == 4
#example
print(is_4_digit(6789))
#this will print out True