当某个输入被执行时,如何使python中断
所以我只是想用python写一些东西,我需要让这个代码循环,直到指定了“是”或“是”,但当没有指定“是”或“是”时,它总是收支平衡。请帮我修一下,谢谢当某个输入被执行时,如何使python中断,python,python-3.x,Python,Python 3.x,所以我只是想用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_confirmatio
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
试着像这样运行代码
print("Please now take the time to fill in your DOB:")
while True:
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? (Yes/No)")
if DOB_confirmation.lower() == "yes":
break
我喜欢做这种类型的循环,它可以替代java中的do while
.lower()
将字符串转换为小写。因此,如果用户输入'YES'或'YES'等,它将读取与'YES'相同的字符串
您也可以使用
.upper()
将字符串转换为大写。希望这能有所帮助。原始输入
未在我的Python版本(3.7.0)中定义,因此我将其替换为常规的输入
。此外,一旦我缩进了所有内容,它似乎工作正常,只需接受“是”
代码:
输出:
================= RESTART: C:/work/stackoverflow/laksjdfh.py =================
Please now take the time to fill in your DOB:
Date of month:asdf
Month:fasd
Year:3232
Please confirm, is this correct?q
Date of month:asdf
Month:fasd
Year:gggf
Please confirm, is this correct?YES
broke (the good way)
>>>
看看你的
,而DOB_确认!=“No”或“No”:
行。你试图说“当确认答案不是肯定的时候,继续问生日”……但你不是这么写的。你也错误地使用了或
试试这个: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?")
请修复您的缩进。特别是对于Python这样的语言,正确的缩进是必要的。感谢所有的帮助,如果没有它,我可能不会得到它。不客气!
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?")