Hey guys
quick question.
I am working on a bit of a project, and I am a little stuck.
is there a command where you can end the program, bring it to a premature close?
for example:
the project I am making is a sort of choose your destiny story game, just to practise using if,else and elif statments.
I want to write an if statement that says
if start == “yes”:
print(“good lets start then”)
elif start == “no”:
print(“well then,” + " " + str(name) + " " + “untill we meet again” )
so in this instance the user is asked if they would like to start the game, if they say yes then it will display the first message. if they say no it will say: well then (player name) untill we meet again. However from here you can just continue the game as if you had said yes.
so my question is, is there a command I can add to that elif statement to make the program end as well, and you have to run it again to play?
Welcome to the forums!
The (included by default in all Python installations) sys
module has an exit function:
import sys
sys.exit(arg)
Where arg
can be a string message or an integer. If an integer is used, 0
is considered ‘successful termination’ while any other positive value is considered ‘abnormal termination’ i.e as a result of an error (source: the Python docs).
Happy coding! 
6 Likes
chur bro, appreciate the help, ill read up on that.
also, forgive me if this is something so obvious, but how do you do that thing that highlights the code you put in. the grey box i mean
2 Likes
hmm, had a read through it all, but seems pretty complicated, and i have not the first clue how to implement such code, so ill leave it till a time when i can understand what it means.
To format code you can use three backticks (these → ` ) and the name of the language, so:
```python
code here
```
Don’t worry if the documentation seems a little bit complex, unfortunately it just is sometimes but all you really need is that codeblock above 
Since the set of tools provided by sys
aren’t part of the Python language by default, we have to import them as part of the sys
package. Since it’s a package provided by default in all Python installation, there’s nothing you need to install, and you can just add this at the top of your code:
import sys
Now we can call that exit function wherever we want to exit the code, for example:
sys.exit('Message to be displayed when exiting here')
Alternatively, since we only need .exit()
, and none of the other functions provided by sys
, it’s not particularly efficient to import the entire sys
module, instead we can only import the particular function that we need and call that without the sys.
prefix:
from sys import exit
exit('Message to be displayed when exiting here')
4 Likes
The sys
module is probably overkill here, but definitely something that you might want to leverage as you get more familiar with Python.
Far easier would be to refactor your game logic into a function, e.g.:
def play_the_game():
# here is the code for your game
and then have the “do you want to play?” check invoke that function if the player says yes:
def play_the_game():
# you said yes! excellent, let's run the game
... code here ...
start = input("Are ye ready to play the game? ")
if start == "yes":
print("Lovely stuff, here we goooooooooooooooooo!")
play_the_game()
else:
print(f"Well then, {name}, I guess you're just a big chicken who ain't ready for a cool adventure! Later!")
If the player doesn’t choose to play the game, Python will come to the end of your program having not invoked the game function and its execution will end on its own.
More complicated use cases may require more complex solutions, such as the use of sys
, but this is a fairly straightforward situation and simpler solutions are (generally) better - so resolving your problem by optimising your program flow is probably a good way to do it. 
5 Likes
I see,
I think I understand what you guys are saying. havnt really learned functions yet though, as i said before, I only made this to practise if statements. but when i learn functions I will keep that code in mind and come back to the project
1 Like
As you get more familiar with the language and how to leverage it, there’ll always be things where you look at it and think “wow, I can write that so much better…”
Just another part of learning. 
3 Likes
Hello everyone, I am also working on a project . It is quite complicated project , have anyone any experienced that can help me in the end program command?