I'm trying to write super simple code where I take input and print it.
userInput = input('enter: ')
print(userInput)
The problem that I'm having is when I'm stopping code while the input line is running, I'm getting a error:
Traceback (most recent call last):
File "C:\Users\97258\PycharmProjects\pythonProject6\tast.py", line 1, in <module>
input('enter: ')
File "<frozen codecs>", line 319, in decode
KeyboardInterrupt
I was given a task to somehow make the code not to crash.
I tried adding the try and catch function but it didn't help, probably because the code stops before the catch can catch it.
I'm only expecting the code to not give me an error and finish with exit code 0 or -1 when I stop the code.
the "try" is correct to add
try:
userInput = input("enter : ")
print(userInput)
but after the try you need an "except" this post has some good responses if you are wondering why. and even has some responses going over why using this would be needed over an "if" statement since its easy to see them one in the same.
so you can add this
except KeyboardInterrupt:
print("program stopped")
this should cover the issues you are having 👍