Python 如何修复while循环
我正在编写一个程序,要求用户输入密码。如果密码与我设置的常量匹配,它将打印一条“成功登录消息”。但是,如果密码不正确,它会给出剩余的猜测次数,并要求用户重试。该程序应在3次错误猜测后结束,但即使使用了3次尝试,它仍会继续询问。我认为问题在我的while循环中,但我不确定 代码:Python 如何修复while循环,python,Python,我正在编写一个程序,要求用户输入密码。如果密码与我设置的常量匹配,它将打印一条“成功登录消息”。但是,如果密码不正确,它会给出剩余的猜测次数,并要求用户重试。该程序应在3次错误猜测后结束,但即使使用了3次尝试,它仍会继续询问。我认为问题在我的while循环中,但我不确定 代码: 条件密码!=密码不足以退出循环(仅当您提供正确的密码时,它才会退出循环)。将条件也添加到while(password!=password and ALLOWED>0)条件密码!=密码不足以退出循环(仅当您提供正确的密码时
条件密码!=密码不足以退出循环(仅当您提供正确的密码时,它才会退出循环)。将条件也添加到while(password!=password and ALLOWED>0)条件密码!=密码不足以退出循环(仅当您提供正确的密码时,它才会退出循环)。同时在while(password!=password and ALLOWED>0)中添加该条件。现在,您永远不会退出while循环。要打破它,请使用
break
关键字。
要完全退出程序,您需要导入sys
和sys.exit()
如果允许==0,我建议将这些添加到您的
语句中。现在您永远不会退出while循环。要打破它,请使用break
关键字。
要完全退出程序,您需要导入sys
和sys.exit()
如果允许,我建议将它们添加到语句==0
中。您需要使用break
退出循环,或者添加第二个条件,否则它将继续运行,直到密码正确为止
因此,要么:
while (password != PASSWORD) and (ALLOWED > 0):
或:
您需要使用break
退出循环,或者添加第二个条件,否则它将继续运行,直到密码正确为止
因此,要么:
while (password != PASSWORD) and (ALLOWED > 0):
或:
将打印(“您已被锁定”)
更改为sys.exit(“您已被锁定”)
(或者退出main
)。请记住导入系统
以使用sys.exit
将打印(“您已被锁定”)更改为sys.exit(“您已被锁定”)
(或者退出主系统
)。记住要使用sys.exit
导入sys,while循环需要正确的密码才能完成,并且没有其他方法可以退出循环。我建议一个中断声明:
def main():
PASSWORD = "apple"
ALLOWED = 3
password = input("Enter the password: ")
while password != PASSWORD :
ALLOWED = ALLOWED - 1
print("Wrong. You have", ALLOWED, "guesses left")
if ALLOWED == 0:
print("You have been locked out")
break
password = input("Enter again ")
print("You have successfully logged into the system")
如果您的程序需要安全,您可能需要进行更多的研究。您的while循环需要正确的密码才能完成,并且没有其他方法可以退出循环。我建议一个中断声明:
def main():
PASSWORD = "apple"
ALLOWED = 3
password = input("Enter the password: ")
while password != PASSWORD :
ALLOWED = ALLOWED - 1
print("Wrong. You have", ALLOWED, "guesses left")
if ALLOWED == 0:
print("You have been locked out")
break
password = input("Enter again ")
print("You have successfully logged into the system")
如果你的程序需要安全,你可能需要做更多的研究。设法让它工作,但只是让它检查是否允许==0,如果它确实打印“你被锁定”,如果允许设法让它工作,但只是让它检查是否允许==0,如果它确实打印“你被锁定”,如果允许,当没有允许的尝试离开时,你没有跳出循环,那么使用
raw_input
代替input
@zengr可能是Python 3当没有允许的尝试离开时,你没有跳出循环,使用raw\u input
代替input
@zengr可能是Python 3break
退出或添加测试时仍会显示“you's loggin”,因为循环后的下一行显示“you have successfully logging the system”
。你需要退出函数,而不仅仅是循环。啊,这是真的。除了解决眼前的问题,我并没有真正专注于其他事情;我的坏。中断
退出或添加测试仍会显示“您已登录”,因为循环后的下一行显示“您已成功登录系统”。你需要退出函数,而不仅仅是循环。啊,这是真的。除了解决眼前的问题,我并没有真正专注于其他事情;我的错。
def main():
PASSWORD = "apple"
ALLOWED = 3
password = input("Enter the password: ")
while password != PASSWORD or ALLOWED <= 0:
ALLOWED = ALLOWED - 1
if ALLOWED > 0: print("Wrong. You have", ALLOWED, "guesses left")
if ALLOWED == 0: print("You have been locked out")
if ALLOWED < 0:
print("You have been locked out")
password = input("Enter again ")
print("You have successfully logged into the system")
main()
def main():
USERNAME = "admin"
PASSWORD = "root"
ATTEMPTS = 3
while ATTEMPTS >= 1:
print("You have",ATTEMPTS,"attempts left")
if ATTEMPTS == 0:
break
user = input("Please enter your username:")
password = input("Now enter your password:")
if user == USERNAME and password == PASSWORD:
print("\nYou have successfully logged into the system")
return
else:
print("\nThis user name or password does not exist\n")
ATTEMPTS -= 1
print("you have been locked out.")
main()