Python 3.x 返回if语句的顶部[Python]
所以我在Python里胡闹。我不知道如何使它,如果你输入一个无效的用户名/密码,然后它返回到循环的顶部,并再次从if语句开始。当我输入无效的用户名或密码时,它不会这样做。那么,有人能帮我解释一下原因吗Python 3.x 返回if语句的顶部[Python],python-3.x,Python 3.x,所以我在Python里胡闹。我不知道如何使它,如果你输入一个无效的用户名/密码,然后它返回到循环的顶部,并再次从if语句开始。当我输入无效的用户名或密码时,它不会这样做。那么,有人能帮我解释一下原因吗 values = ["User1", "123", "User2", "321", "User3", "132"] print("Please log in to the program") username = input("Please enter your username: ") att
values = ["User1", "123", "User2", "321", "User3", "132"]
print("Please log in to the program")
username = input("Please enter your username: ")
attempts = 3
while attempts >=1:
if username in values:
place = values.index(username)
passposition = int(place + 1)
print("Your username is valid.")
password = input("Now please enter the password to your account. Once again, passwords are case sensitive: ")
if password == values[passposition]:
print("The password is correct, welcome to the program")
else:
print("The password was invalid, please try again")
attempts -= 1
print("You now have ", attempts, " more attempts to log in with a valid username and password")
else:
print("Your username is invalid, please try again")
尝试次数-=1
打印(“您现在有”,尝试,“更多使用有效用户名和密码登录的尝试”)这对您有用吗
values = ["User1", "123", "User2", "321", "User3", "132"]
attempts = 3
print("Please log in to the program")
while attempts >=1:
#You need to ask for the username again, if you don't do this the variable is always assigned with a invalid value
#That's why it must be inside the loop
username = input("Please enter your username: ")
if username in values:
place = values.index(username)
passposition = int(place + 1)
print("Your username is valid.")
password = input("Now please enter the password to your account. Once again, passwords are case sensitive: ")
if password == values[passposition]:
print("The password is correct, welcome to the program")
#Once you have the right credentials you need to break the loop, if you comment the break you'll see that the programs asks again for the password
break
else:
print("The password was invalid, please try again")
attempts -= 1
print("You now have ", attempts, " more attempts to log in with a valid username and password")
else:
print("Your username is invalid, please try again")
attempts -= 1
print("You now have ", attempts, " more attempts to log in with a valid username and password")
您正在特别寻找
continue
语句,但您也应该阅读“谢谢”,这很有效,但您能解释一下您做了什么吗?当然,我会将它们添加为注释。如果对你有用,请接受答案。