为了账号安全,请及时绑定邮箱和手机立即绑定

虽然循环不会结束python

虽然循环不会结束python

烙印99 2021-09-11 10:25:16
在函数选项完成后,while 循环仍然继续,即使 update 与它需要的字符串之一相同。满足更新条件后如何停止while循环while update.lower() != "tests" or "lessons" or "adjust":     update = input("Do you want to update content from 'lessons' or 'tests'. Or do you want to 'adjust' what you aren't confident with. respond with 'adjust' 'lesson' or 'tests'").lower()     if update.lower() == "tests" or "lessons" or "adjust":         options()
查看完整描述

2 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

你需要

while update.lower() not in ["tests", "lessons", "adjust"]

你写的内容被解析为

{update.lower() != "tests"} OR {"lessons"} OR {"adjust"}

(括号只是为了显示语言如何对术语进行分组)

由非空字符串组成的条件在python中始终为真,因此“课程”部分将始终为真,while循环永远不会。


查看完整回答
反对 回复 2021-09-11
?
狐的传说

TA贡献1804条经验 获得超3个赞

就是因为or你觉得他们的工作,在你的榜样,他们被解释为不同条件下在你的代码不能正常工作update.lower() != "tests",lessons并且adjust因此近两年一直在为处理True所以这个循环将永远不会结束。相反,你应该这样做:


while update.lower() not in ["tests", "lessons", "adjust"]:

     update = input("Do you want to update content from 'lessons' or 'tests'. Or do you want to 'adjust' what you aren't confident with. respond with 'adjust' 'lesson' or 'tests'").lower()


     if update.lower() in ["tests", "lessons", "adjust"]:

         options()


查看完整回答
反对 回复 2021-09-11
  • 2 回答
  • 0 关注
  • 188 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号