命令,直到出现正确答案为止

2024-05-16 04:11:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在python上发出一个命令,要求用户输入我弟弟的正确年龄。如果输入23岁,程序将终止,并说“干得好”。否则,用户会再次询问我弟弟的年龄。我不明白如何制作这样一个程序。下面是我最好的猜测:

age = input("what is the age of your brother? ")
while age = 23:
    print ("correct answer entered")
    else:
        print ("incorrect answer entered")

我得到一个语法错误


Tags: ofthe用户answer命令程序inputage
1条回答
网友
1楼 · 发布于 2024-05-16 04:11:59

您需要询问循环中的年龄,并在正确时结束循环。因为input()返回一个字符串,所以需要将它与字符串进行比较,而不是整数。要比较事物,需要使用==,而不是=。你知道吗

while True:
    age = input("what is the age of your brother? ")
    if age == '23':
        print("correct answer entered")
        break
    else:
        print("incorrect answer entered")

相关问题 更多 >