我想问用户是否要退出或再次写信
import pyttsx3
robot = pyttsx3.init()
while True:
robot.say(input('Write something that you need me to say:\n'))
robot.runAndWait()
n = (input('if you need to quit type : quit , if you need to write again
press any button\n'))
if n == 'Quit' or 'quit' or 'QUIT':
break
else:
continue
我想问用户是否要退出或再次写入,但是当我运行代码时,它会以任何方式打破循环。
import pyttsx3
robot = pyttsx3.init()
while True:
robot.say(input('Write something that you need me to say:\n'))
robot.runAndWait()
n = (input('if you need to quit type : quit , if you need to write again
press any button\n'))
if n == 'Quit' or 'quit' or 'QUIT':
break
else:
continue
I want to ask the user if he want to quit or write again but when I run my code it breaks the loop any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这些机器人线在做什么,所以我对它们进行了评论,因为它们与我在您的代码中发现的错误无关。我通过纠正您对
或
语句的使用来工作并使用案例不敏感检查:说明:
您写了
,如果n =='quit'或'退出'或'quit'
- 的意思是“如果n等于'退出',或者'quit'是真实的(它是)或'quit'是真实的(它是),请这样做:break break < /code>“因此,您看到的终止。如果要正确使用
或
,则表达式需要读取:Not sure what those robot lines are doing so I commented them out since they are not related to the bug I found in your code. I got this to work by correcting your use of
or
statements and using case insensitive check instead:Explanation:
You wrote
if n == 'Quit' or 'quit' or 'QUIT'
- which means "If n equals 'Quit' or if 'quit' is truthy (it is) or 'QUIT' is truthy (it is), proceed this way:break
" hence the termination you saw. If you wanted to useor
correctly, the expression would need to read: