Python 为什么我的打印语句没有运行在else下面?

Python 为什么我的打印语句没有运行在else下面?,python,python-3.x,Python,Python 3.x,只是一个python初学者。我正在尝试建立一个密码系统,用户可以输入尝试次数来输入密码,或者键入-1进行无限次尝试。密码是999。例如,假设我们为尝试输入密码的次数输入3次尝试。所有的猜测都是错误的。但是,屏幕上没有显示任何内容,而不是打印错误的密码。我该如何解决这个问题?除了上次打印没有显示之外,没有什么不合适的了。因为打印之前的“继续”从未执行打印 如果与结束条件兼容,continue要求执行for的下一轮 注意:要获得预期的行为,您不仅必须删除else分支中的continue,还必须删除b

只是一个python初学者。我正在尝试建立一个密码系统,用户可以输入尝试次数来输入密码,或者键入-1进行无限次尝试。密码是999。例如,假设我们为尝试输入密码的次数输入3次尝试。所有的猜测都是错误的。但是,屏幕上没有显示任何内容,而不是打印错误的密码。我该如何解决这个问题?除了上次打印没有显示之外,没有什么不合适的了。

因为打印之前的“继续”从未执行打印

如果与结束条件兼容,continue要求执行for的下一轮

注意:要获得预期的行为,您不仅必须删除else分支中的continue,还必须删除break,以便:

tries=int(input("Enter the number of times you wish to enter the password."))

password=[]

if tries == -1:
    while(True):
        password_guess=int(input("Enter the password (hint: it is an integer): "))
        password.append(password_guess)
        if password_guess == 999:
            print("correct password")
            break
        else:
            continue
else:
    #n=int(input("enter the amount of tries you want to have"))
    password=[0 for x in range(tries)]
    for i in range(tries):
        password[i]=int(input("Enter the password (hint: it is an integer): "))
        if 999 in password:
            print("correct password")
            break
        else:
            continue
            print("incorrect password")
            break
if 999 in password:
    print("correct password")
    break
print("incorrect password")
password[i]=int(input("Enter the password (hint: it is an integer): "))
if 999 in password:
因为then分支有一个断点,所以您也可以执行以下操作:

if 999 in password:
    print("correct password")
    break
else:
    print("incorrect password")
无论如何,我希望:

tries=int(input("Enter the number of times you wish to enter the password."))

password=[]

if tries == -1:
    while(True):
        password_guess=int(input("Enter the password (hint: it is an integer): "))
        password.append(password_guess)
        if password_guess == 999:
            print("correct password")
            break
        else:
            continue
else:
    #n=int(input("enter the amount of tries you want to have"))
    password=[0 for x in range(tries)]
    for i in range(tries):
        password[i]=int(input("Enter the password (hint: it is an integer): "))
        if 999 in password:
            print("correct password")
            break
        else:
            continue
            print("incorrect password")
            break
if 999 in password:
    print("correct password")
    break
print("incorrect password")
password[i]=int(input("Enter the password (hint: it is an integer): "))
if 999 in password:
这很奇怪,因为它足以:

tries=int(input("Enter the number of times you wish to enter the password."))

password=[]

if tries == -1:
    while(True):
        password_guess=int(input("Enter the password (hint: it is an integer): "))
        password.append(password_guess)
        if password_guess == 999:
            print("correct password")
            break
        else:
            continue
else:
    #n=int(input("enter the amount of tries you want to have"))
    password=[0 for x in range(tries)]
    for i in range(tries):
        password[i]=int(input("Enter the password (hint: it is an integer): "))
        if 999 in password:
            print("correct password")
            break
        else:
            continue
            print("incorrect password")
            break
if 999 in password:
    print("correct password")
    break
print("incorrect password")
password[i]=int(input("Enter the password (hint: it is an integer): "))
if 999 in password:
您的初始化:

也很奇怪,因为您可以执行password=[]并替换password[i]=int。。。通过密码。appendint…:

在你的第一个

也没用

因此,您可以:

else:
    continue
当然,您也可以在这两种情况下使用password_guess,或在这两种情况下都使用none来获得相似的代码,或者不使用函数编写两次相同的代码:

tries=int(input("Enter the number of times you wish to enter the password."))

password=[]

if tries == -1:
    while(True):
        password_guess=int(input("Enter the password (hint: it is an integer): "))
        password.append(password_guess)
        if password_guess == 999:
            print("correct password")
            break
else:
    for i in range(tries):
        password.append(int(input("Enter the password (hint: it is an integer): ")))
        if 999 == password[i]:
            print("correct password")
            break
        else:
            print("incorrect password")

print("attempts : ", password)
假设全部在p.py中执行:


因为打印之前的“继续”,所以打印永远不会成功executed@AbhinavMathur休息时间不在其他部分谢谢您的回复!但是,每次输入新密码时,这都会给我错误的密码。在计算机以不正确的密码回复之前,如何完成所有错误的尝试?谢谢你的帮助。