Reputation: 9
When i press 5 for quit it comes up as an error but does not work
For some reason
import sys
sys.exit()
does not work
def ask (user) :
if user == 1:
user = input('select a file with an ASCII art image ')
f = open(user, 'r')
if f.mode == 'r':
showart = f.read()
print(showart)
print("You will be returned to the menu")
elif user == 2:
print('hi')
elif user == 3:
print ('hi')
elif user == 4:
print("hi")
elif user == 5:
print('goodbye')
import sys
sys.exit()
while (True) :
print("1 - Display ASCII art ")
print("4 - convert RLE option ")
print("5 - Quit ")
print(" ")
print(" ")
print(" ")
try:
user=int(input("Select an integer between 1 and 5 : "))
if (user<5 and user > 1) :
ask(user)
else:
user=int(input("Please enter a number between 1 and 5 : "))
while (user > 5 or user < 1) :
user=int(input("Please enter a number between 1 and 5 : "))
ask (user)
except:
print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')
it prints goodbye but it does not quit the program
Upvotes: 0
Views: 195
Reputation: 6159
Because sys.exit()
throws an error, which is caught by the except
and runs again due to the while True
As mentionner in the comments, you can simply add the Exception
type, as SystemExit
is not a subtype of Exception
it won't be catch, and then you can exit the program properly
def ask(user):
if user == 1:
user = input('select a file with an ASCII art image ')
f = open(user, 'r')
if f.mode == 'r':
showart = f.read()
print(showart)
print("You will be returned to the menu")
elif user == 2:
print('hi')
return 0
elif user == 3:
print('hi')
elif user == 4:
print("hi")
while True:
print("1 - Display ASCII art ")
print("4 - convert RLE option ")
print("5 - Quit ")
print(" ")
print(" ")
print(" ")
try:
user = int(input("Select an integer between 1 and 5 : "))
"""
If user enter value which is 5 then print 'goodbye'
as you expect and break the while loop. So it's no
longer running.
"""
if user == 5:
print('goodbye')
break
"""
5 > user > 0 mean match only 4, 3, 2, 1
this loop will continue when the user entered an
integer value which is not belongs to 4, 3, 2, 1.
Otherwise it goes to else statement and execute
ask(user) function.
"""
if not 5 > user > 0: # [..., -1, 0, 5, 6, ...]
continue
else:
ask(user)
except Exception:
print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')
Upvotes: 2
Reputation: 8586
I just change your code, but I don't know it match with your requirements and explain what I did in the comment of the code.
def ask(user):
if user == 1:
user = input('select a file with an ASCII art image ')
f = open(user, 'r')
if f.mode == 'r':
showart = f.read()
print(showart)
print("You will be returned to the menu")
elif user == 2:
print('hi')
return 0
elif user == 3:
print('hi')
elif user == 4:
print("hi")
while True:
print("1 - Display ASCII art ")
print("4 - convert RLE option ")
print("5 - Quit ")
print(" ")
print(" ")
print(" ")
try:
user = int(input("Select an integer between 1 and 5 : "))
"""
If user enter value which is 5 then print 'goodbye'
as you expect and break the while loop. So it's no
longer running.
"""
if user == 5:
print('goodbye')
break
"""
5 > user > 0 mean match only 4, 3, 2, 1
this loop will continue when the user entered an
integer value which is not belongs to 4, 3, 2, 1.
Otherwise it goes to else statement and execute
ask(user) function.
"""
if not 5 > user > 0: # [..., -1, 0, 5, 6, ...]
continue
else:
ask(user)
except:
print('***SORRY, YOUR OPTION DID NOT WORK***\n ***PLEASE SELECT ANOTHER NUMBER***')
If you didn't get anything, please comment to this answer and I'll help you.
Upvotes: 0
Reputation: 1343
You are catching sys.exit
Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.
https://docs.python.org/3/library/sys.html#sys.exit
Upvotes: 0