虽然循环不会结束python

在函数选项完成后,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()


烙印99
浏览 189回答 2
2回答

临摹微笑

你需要while update.lower() not in ["tests", "lessons", "adjust"]你写的内容被解析为{update.lower() != "tests"} OR {"lessons"} OR {"adjust"}(括号只是为了显示语言如何对术语进行分组)由非空字符串组成的条件在python中始终为真,因此“课程”部分将始终为真,while循环永远不会。

狐的传说

就是因为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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python