+ 2

Can it work? To open phones?

import random a1=" " a2=" " a3=" " a4=" " b = 2728 while True: a1=random.randint(0,9) a2=random.randint(0,9) a3=random.randint(0,9) a4=random.randint(0,9) print(f"{a1}{a2}{a3}{a4}") if (f"{a1}{a2}{a3}{a4}")==b: break

28th Nov 2023, 7:19 PM
adam sdiri
adam sdiri - avatar
6 ответов
+ 3
from random import randint b = '2728' cycles = 0 while True: n = f'{randint(0, 10000):04}' print(n) cycles += 1 if n == b: break print(f"Unlocked in {cycles} cycles.")
29th Nov 2023, 3:14 AM
Bob_Li
Bob_Li - avatar
+ 3
No, you're trying to compare a number with a string.
28th Nov 2023, 7:31 PM
Solo
Solo - avatar
+ 3
Bob_Li Nice👏
29th Nov 2023, 6:06 AM
StuartH
StuartH - avatar
+ 2
What is supposed to work? Check the spelling and the condition. not "some string" will always be false. Also, you compare a string to an integer.
28th Nov 2023, 7:33 PM
Lisa
Lisa - avatar
+ 2
Try this: from random import randint b = 2728 cycles = 0 while True: a1=randint(0,9) a2=randint(0,9) a3=randint(0,9) a4=randint(0,9) print(f"{a1}{a2}{a3}{a4}") cycles += 1 if (f"{a1}{a2}{a3}{a4}")==str(b): break print(f"Unlocked in {cycles} cycles.")
28th Nov 2023, 8:21 PM
StuartH
StuartH - avatar
+ 1
A few things I'd suggest, firstly in Python you don't need to declare your variables ahead of time in this context, especially not as strings when you intend to use them as integers. If you import only randint like this: from random import randint you can just call randint(0,9) which makes your code more streamlined. Lastly, dont create an f string and compare to an integer, it will always be false. Either construct a 4 digit integer to compare with b, or convert b to a string before the comparison
28th Nov 2023, 8:16 PM
StuartH
StuartH - avatar