-1

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()
John Burton
  • 11
  • 1
  • 5
  • On an uncaught `EOFError` the application should quit with a traceback already. Do you want to have something different than that? – Klaus D. Apr 20 '22 at 03:49
  • @KlausD. I want it to quit quietly with no errors – John Burton Apr 20 '22 at 03:53
  • 1
    Then you have to [handle the exception](https://docs.python.org/3/tutorial/errors.html). – Klaus D. Apr 20 '22 at 03:54
  • Does this answer your question? [How to terminate loop gracefully when CTRL+C was pressed in python](https://stackoverflow.com/questions/24426451/how-to-terminate-loop-gracefully-when-ctrlc-was-pressed-in-python) – Nitesh Tosniwal Apr 20 '22 at 04:07

1 Answers1

0

I fixed it with

try:
  while True:
      userInput = input()
      #any other work here
except EOFError as e:
  print(e)
Soroosh Sorkhani
  • 66
  • 1
  • 3
  • 15
John Burton
  • 11
  • 1
  • 5