如何询问什么是什么(输入)然后如果他们弄错了它会再次询问他们(while 循环)

how to ask what something is (input) then if they get it wrong it'll ask them again (while loop)

我正在参加计算方面的 GCSE,并为了好玩而开始编写新代码,但我遇到了像菜鸟一样的麻烦。我正在制作一个代码,询问他们密码是什么,如果他们输入错误,它将再次询问,直到满足要求,我已经在互联网上查看过,它让我对我检查过的所有网站进行了倒计时。到目前为止,这是我的代码...

password = 1234
passinput = (input("what is your password? "))

我尝试了几种方法,例如...

while True:

while False:

但我不明白如何正确使用它们。我在 class 学过,但很容易忘记。我希望代码继续询问用户,直到他们输入“1234”(密码)

请帮忙。

简单的答案是:

while True:
    answer = input('password?')
    if answer == password:
        break

对于更复杂的需求(或对选项的更好理解),请参阅 Asking the user for input until they give a valid response

更简单的方法

password = 1234
while int(input()) != password: pass

捷径:

while True:
    if input("what's the password? ") == "1234":
        break

或更短:

while input("what's the password? ") != "1234":
    pass

或者,如果您坚持使用单线:

while input("what's the password? ") != "1234": pass