如何通过控制D退出Python计划?

发布于 2025-01-22 10:40:01 字数 231 浏览 3 评论 0原文

我有一个Python程序,该程序使用一个WALE循环检查用户输入。我希望在用户输入输入时继续运行,但是当用户输入CTRL d时退出。目前,我在键入ctrl d时会得到eoferror。我是Python的新手,因此有关如何解决此问题的任何建议真的很有帮助。

这是我的代码的一个非常简单的示例:

while True:
  userInput = input()
  ...
exit()

I have a python program that uses a while loop to check for a user input. I want it to keep running while a user enters an input, but quit when the user enters ctrl d. Currently, I get EOFError when typing ctrl d. I am new to python so any advice on how to fix this would be really helpful.

Here is a very simple example of my code:

while True:
  userInput = input()
  ...
exit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

贱人配狗天长地久 2025-01-29 10:40:01

我用

try:
  while True:
      userInput = input()
      #any other work here
except EOFError as e:
  print(e)

I fixed it with

try:
  while True:
      userInput = input()
      #any other work here
except EOFError as e:
  print(e)
不如归去 2025-01-29 10:40:01

出口()关键字也很有用。它还提高了可读性。

while True:
    try:
        user_input = input()
    except EOFError:
        exit()

The exit() keyword is also useful. It also improves readability.

while True:
    try:
        user_input = input()
    except EOFError:
        exit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文