Please help me to solve this problem in this problem how to check user entered number is 4 digit or not? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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")

29th Aug 2021, 1:03 PM
Mufeeda
Mufeeda - avatar
5 Antworten
+ 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... ...
29th Aug 2021, 5:39 PM
Lothar
Lothar - avatar
+ 4
“if num >= 1000 and num <= 9999”?
29th Aug 2021, 1:15 PM
Tim
Tim - avatar
+ 3
Would you prefer if it's coded this way https://code.sololearn.com/clkaEK4BWSTX/?ref=app
29th Aug 2021, 1:42 PM
Tim
Tim - avatar
+ 2
Or you could check the length of the input string
29th Aug 2021, 1:37 PM
Lisa
Lisa - avatar
0
#use this: def is_4_digit(num): return len(str(num)) == 4 #example print(is_4_digit(6789)) #this will print out True
30th Aug 2021, 12:07 AM
⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀ - avatar