首页 > 解决方案 > 进行特定输入时如何使我的python中断

问题描述

所以我只是想用python写一些东西,我需要让这个代码循环,直到指定“是”或“是”,但即使没有指定“是”或“是”,它也会继续中断。请帮我解决这个问题,并在此先感谢。

print("Please now take the time to fill in your DOB:")
DOB_day = raw_input ("Date of month:")
DOB_month = raw_input ("Month:")
DOB_year = raw_input ("Year:")

DOB_confirmation = raw_input ("Please confirm, is this correct?")


while DOB_confirmation != "No" or "no":
    DOB_day = raw_input ("Date of month:")
    DOB_month = raw_input ("Month:")
    DOB_year = raw_input ("Year:")
    DOB_confirmation = raw_input ("Please confirm, is this correct?")
    if DOB_confirmation == "Yes" or "yes":
        break

标签: pythonpython-3.x

解决方案


看看你的while DOB_confirmation != "No" or "no":线路。您试图说“虽然确认答案不是肯定的,但请继续询问生日”……但这不是您写的。你也用or错了。

试试这个:while DOB_confirmation.lower() != "yes":。这实际上是说“虽然用户没有输入任何形式的'YES'”,这就是你要找的。

您可以消除最后的if语句 - 它被while循环覆盖。

尝试这个:

print("Please now take the time to fill in your DOB:")
DOB_day = input("Date of month:")
DOB_month = input("Month:")
DOB_year = input("Year:")

DOB_confirmation = input("Please confirm, is this correct?")


while DOB_confirmation.lower() != "yes":
      DOB_day = input("Date of month:")
      DOB_month = input("Month:")
      DOB_year = input("Year:")
      DOB_confirmation = input("Please confirm, is this correct?")

推荐阅读