我的代码的一部分无法在 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")


蝴蝶刀刀
浏览 67回答 2
2回答

繁星淼淼

尝试更换pass1&nbsp;=&nbsp;input("Please&nbsp;insert&nbsp;your&nbsp;phone&nbsp;password:")和pass1&nbsp;=&nbsp;int(input("Please&nbsp;insert&nbsp;your&nbsp;phone&nbsp;password:"))&nbsp;#&nbsp;turns&nbsp;pass1&nbsp;into&nbsp;an&nbsp;integer

蛊毒传说

这是一个更好的版本(没有双 while 循环并带有 int 转换):#importsimport time, random#Welcomeprint("Welcome to Password Guess!")pass1 = int(input("Please insert your phone password:"))#Start systemguessclock = 0start1 = 1i = Truewhile i:&nbsp; &nbsp; guess1 = random.randint(1, 2)&nbsp; &nbsp; guessclock += 1&nbsp; &nbsp; print(guess1)&nbsp; &nbsp; if pass1 == guess1:&nbsp; &nbsp; &nbsp; &nbsp; i = Falseprint("Password guessed")print("It took", guessclock)print("Attempts")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python