player_input = '' # This has to be initialized for the loop
while player_input != 0:
player_input = str(input('Roll or quit (r or q)'))
if player_input == q: # This will break the loop if the player decides to quit
print("Now let's see if I can beat your score of", player)
break
if player_input != r:
print('invalid choice, try again')
if player_input ==r:
roll= randint (1,8)
player +=roll #(+= sign helps to keep track of score)
print('You rolled is ' + str(roll))
if roll ==1:
print('You Lose :)')
sys.exit
break
I am trying to tell the program to exit if roll == 1
but nothing is happening and it just gives me an error message when I try to use sys.exit()
This is the message that it shows when I run the program:
Traceback (most recent call last):
line 33, in <module>
sys.exit()
SystemExit
exit() function allows the developer to exit from Python. The exit function takes an optional argument, typically an integer, that gives an exit status. Zero is considered a “successful termination”.
Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.
The standard convention for all C programs, including Python, is for exit(0) to indicate success, and exit(1) or any other non-zero value (in the range 1.. 255) to indicate failure. Any value outside the range 0.. 255 is treated modulo 256 (the exit status is stored in an 8-bit value).
exit is a helper for the interactive shell - sys. exit is intended for use in programs. The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace (e.g. exit ).
I think you can use
sys.exit(0)
You may check it here in the python 2.7 doc:
The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like.
sys.exit()
raises a SystemExit
exception which you are probably assuming as some error. If you want your program not to raise SystemExit but return gracefully, you can wrap your functionality in a function and return from places you are planning to use sys.exit
you didn't import sys in your code, nor did you close the () when calling the function... try:
import sys
sys.exit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With