我的代码的一部分在while循环中不起作用



我不知道为什么,但它在猜测密码,但当猜测正确时,它并没有结束程序。非常感谢您的帮助!

#imports
import time, random
#Welcome
print("Welcome to Password Guess!")
pass1 = input("Please insert your phone password:")
#Start system
guessclock = 0
start1 = 1
i = 1
while i < 2:
while i < 2:
guess1 = random.randint(1, 2)
guessclock += 1
print(guess1)
if pass1 == guess1:
i = 3
print("Password guessed")
print("It took", guessclock)
print("Attempts")

尝试替换

pass1 = input("Please insert your phone password:")

带有

pass1 = int(input("Please insert your phone password:")) # turns pass1 into an integer

这里有一个更好的版本(没有双while循环和int转换(:

#imports
import time, random
#Welcome
print("Welcome to Password Guess!")
pass1 = int(input("Please insert your phone password:"))
#Start system
guessclock = 0
start1 = 1
i = True
while i:
guess1 = random.randint(1, 2)
guessclock += 1
print(guess1)
if pass1 == guess1:
i = False
print("Password guessed")
print("It took", guessclock)
print("Attempts")

最新更新