为了账号安全,请及时绑定邮箱和手机立即绑定

我的代码的一部分无法在 while 循环中工作

我的代码的一部分无法在 while 循环中工作

蝴蝶刀刀 2023-12-12 15:11:19
我不知道为什么,但它正在猜测密码,但当它猜测正确时,它并没有结束程序。任何帮助深表感谢!#importsimport time, random#Welcomeprint("Welcome to Password Guess!")pass1 = input("Please insert your phone password:")#Start systemguessclock = 0start1 = 1i = 1while 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")
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

尝试更换

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

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


查看完整回答
反对 回复 2023-12-12
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

这是一个更好的版本(没有双 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")


查看完整回答
反对 回复 2023-12-12
  • 2 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信