python - 进行特定输入时如何使我的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
解决方案
看看你的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?")
推荐阅读
- python - 循环遍历电子邮件的数据帧会产生 smtplib.SMTPRecipientsRefused 错误
- java - 如何在此方法中将 createCriteria(已弃用)更改为 CriteriaQuery?
- javascript - 如何使用 JS、Chart.js 和 PHP 计算最佳拟合的非线性线(回归线)?
- ios - 命令 PhaseScriptExecution 失败,退出代码为非零(反应本机 0.64.0,xcode 12.4)
- c++ - 如何根据值比较启用模板?
- javascript - 组件数组过滤器问题
- javascript - fetch() 仅在浏览器中设置 4 个 cookie 中的 1 个
- python - 如何在 Keras 中使用文件夹中的图像作为目标?
- javascript - 使用 Jayq 将 Resize 事件绑定到 Clojurescript
- javascript - 如何遍历对象键的值,如果匹配,则将其与另一个值进行比较,而不是将其更改为另一个值