我想问用户是否要退出或再次写信

发布于 2025-02-11 20:04:29 字数 369 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2025-02-18 20:04:29

不确定这些机器人线在做什么,所以我对它们进行了评论,因为它们与我在您的代码中发现的错误无关。我通过纠正您对语句的使用来工作并使用案例不敏感检查:

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.lower() == 'quit':
    print("quitting...\n")
    break
  else:
    print("continuing...\n")
    continue

说明

您写了,如果n =='quit'或'退出'或'quit' - 的意思是“如果n等于'退出',或者'quit'是真实的(它是)或'quit'是真实的(它是),请这样做:break break < /code>“因此,您看到的终止。如果要正确使用,则表达式需要读取:

if n == 'Quit' or n == 'quit' or n == 'QUIT

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:

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.lower() == 'quit':
    print("quitting...\n")
    break
  else:
    print("continuing...\n")
    continue

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 use or correctly, the expression would need to read:

if n == 'Quit' or n == 'quit' or n == 'QUIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文