Foley Foley - 6 months ago 12
Python Question

Why is my Python code doing a sys.exit after I choose my option

The Code.
if I enter

a/b/c/q
it defaults to
sys.exit


I've got literally no idea why it does this, so any suggestions are really welcome.

import sys
print(' Option A ')
print(' Option B ')
print(' Option C ')
answer = input(' ').lower

if answer == 'a':

#code is here
sys.exit

if answer == 'b':

#more code is here
sys.exit

elif answer == 'c':

#more code here
sys.exit


elif answer == 'q':
print ("exit program')
sys.exit


Any help would be amazing! (I'm fairly new to python, version 3.5.1)

Answer

You're missing the indentation:

if answer == 'a':

#code is here
sys.exit

Should be:

if answer == 'a':
  #code is here
  sys.exit()

You'll need to do this for your other if and elif as well.

Note that the addition of the () since exit is a function, not an attribute of sys.

Comments