gpt4 book ai didi

python - 如何在语句为假时重复代码 [Python]

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:31 24 4
gpt4 key购买 nike

我真的很想了解如何在 Python 中的语句为 false 时重复我的代码。我只是想知道,如果错误,我该如何让 Python 再次询问用户名和密码?这是我的代码:

print("Enter username")
username = input()
print("Enter password")
pword = input()
if username=="Shadow" and pword=="ItzShadow":
print("Access granted to Shadow!")
else:
print("Wrong username and / or password")

我的意思是,如果其中一个是假的,我如何让 Python 再次询问用户名和密码?

最佳答案

您需要将您的代码包含在一个循环中。像这样:

def get_user_pass():
print("Enter username")
username = input()
print("Enter password")
pword = input()
return username, pword

username, pword = get_user_pass()
while not(username=="Shadow" and pword=="ItzShadow"):
print("Wrong username and / or password")
username, pword = get_user_pass()

print("Access granted to Shadow!")

关于python - 如何在语句为假时重复代码 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com