How to correctly exit a program in python

I am a high school student and I am starting to learn coding in python. I watched the video tutorials, but I canโ€™t figure out how to make the game exit if you type q. that's what i have.

print('How old do you thing Fred the Chicken is?') number = 17 Quit = q run = 17 while run: guess = int(input('Enter What You Think His Age Is....')) print('How old do you thing Fred the Chicken is?') number = 17 Quit = 'q' run = 17 while run: guess = int(input('Enter What You Think His Age Is....')) if guess == number: print('Yes :D That is his age...') run = False elif guess < number: print('No, Guess a little higher...') elif guess > number: print('No, Guess a little lower....') print('Game Over') print('Press Q to Quit') if run == False: choice = input('Press Q to Quit') if choice == 'q' import sys exit(0) 
+8
python string quit
source share
3 answers

Getting Q as input

 Quit = int(input('Press Q to Quit') 

You request Q as input, but only accept int . So remove the int part:

 Quit = input('Press Q to Quit') 

Now Quit will be what the user enters, so let instead of True check "Q":

 if Quit == "Q": 

Instead of sys.exit(0) you can just end your scan with break or just return if you are in a function.

In addition, I do not recommend the name "Quit" for a variable that simply stores user input, as this will end up confusing.

And remember that indentation is important in Python, so it should be:

 if run == False: choice = input('Press Q to Quit') if choice == "Q": # break or return or.. import sys sys.exit(0) 

It can only be a copy / paste error.

Indentation and Syntax

I fixed the indentation and removed the extraneous code (since you are duplicating the outer loop and some of the print statements) and got this:

 print('How old do you thing Fred the Chicken is?') number = 17 run = True while run: guess = int(input('Enter What You Think His Age Is....t')) if guess == number: print('Yes :D That is his age...') run = False elif guess < number: print('No, Guess a little higher...') elif guess > number: print('No, Guess a little lower....') if run == False: print('Game Over') choice = input('Press Q to Quit') if choice == 'q' break 

This gave me a syntax error:

blong @ubuntu: ~ $ python3 chicken.py
File chicken.py, line 23
if choice == 'q'
^
Syntax Error: invalid syntax

So, Python says that something is wrong after the if . If you look at the other if , you will notice that at the end of this one is missing : so change it to:

 if choice == 'q': 

So, with this change, the program starts up and seems to do what you want.

Some suggestions

  • Your instructions say, โ€œPress Q to exit,โ€ but you actually accept โ€œqโ€ to exit. You can accept both. Python has an operator called or , which takes two truth values โ€‹โ€‹( True or False ) and returns True if either of them is True (in fact, it is more than True and False , see the documentation if you are interested).

    Examples:

     >> True or True True >>> True or False True >>> False or True True >>> False or False False 

    So, we can set Q or q with if choice == "Q" or choice == "q":

    Another option is to convert the string to lowercase and check only Q using if choice.lower() == "q": If choice was Q, he would first convert it to q (using .lower() ), then perform a comparison.

  • Your number is always 17. Python has a random.randint () function that will give you a random number that can make the game more fun. For example, this would make the chicken age between 5 and 20 (inclusive):

     number = random.randint(5, 20) 
+10
source share

There are many ways to get out of certain things. For loops, break used, and for functions you can use return . However, for programs, if you want to exit your program before the interpretation is complete (end of script), there are two different types of exit() functions. There is sys.exit() , which is part of the sys module, and there is exit() and quit() , which is inline. However, sys.exit() is not intended for programs in IDLE (python interactive), and the built-in functions exit() and quit() are intended for use in IDLE.

+1
source share

You can use the break statement to exit the while :

 while True: guess = int(input("...") # ...rest of your game logic... # Allow breaking out of the loop choice = input("Enter Q to quit, or press return to continue") if choice.lower() == "q": break 

This means that the interpreter exits the while , continues to search for more commands to run, but reaches the end of the file! Python will exit automatically - no need to call sys.exit

(as an aside, you rarely have to use sys.exit , it really just used to set the non-zero exit status , and not to control how your program works, for example, while acceleration)

Finally, the main problem with the code you entered is indentation - you need to put the appropriate number of spaces at the beginning of each line, where you have something like:

 if run == False: choice = input('Press Q to Quit') 

It should be like this:

 if run == False: choice = input('Press Q to Quit') 

Same as while , for , if , etc.

+1
source share

All Articles