How to exit from Python

I have this problem.

As you can see, I’m trying to catch some exceptions. Everthing is working fine except the ‘EOFError’ (img 1). What I want to do is allow the user to quit the game anytime he wants.

It worked as I expected in another while loop (img 2). The only difference I can see is that the ‘answer’ has a type of string, and ‘game’ has a type of int. But in the middle of the process where I’m expecting the user to give me the answer, if he decide to quit, Ctrl+D works fine. But it works just there.

The problem begins when I ask the user which game he wants to play, but Ctrl+D doesn’t work there. So I’m getting this error message:

During handling of the above exception, another exception occurred:
TypeError: ‘str’ object is not callable

Do you guys have any idea about what I’m missing here?
P.S: I removed the 0 from exit() on purpose. Just trying to figure out what is going on.

img2

when I ran it, it said the variable ‘invalid’ is the problem.
Maybe print(“invalid”)

1 Like

Hello Nellie. Thanks for your comment.
This is just a piece/block of the entire code I wrote.

https://github.com/pyception/ProjetoEducativo

Take a look on my first project and give me your feedback if possible.

I’m using Python3.6 and you will need to install the termcolor (pip install termcolor). Just run play.py and I hope it works

1 Like

The exit() comes from the sys module so first make sure you import sys and do this:

sys.exit(0)

Next, it looks like you maybe have a string variable named “exit” so when you try to call exit() it tries to call the string.

Simply changing to use sys.exit(0) will work.