+ 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
6 Réponses
+ 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.")
+ 3
No, you're trying to compare a number with a string.
+ 3
Bob_Li Nice👏
+ 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.
+ 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.")
+ 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