在选择我的选项后,为什么我的Python代码在执行sys.exit

时间:2016-05-02 00:23:24

标签: python input

守则。  如果我输入a/b/c/q,则默认为sys.exit

我真的不知道为什么会这样做,所以任何建议都非常受欢迎。

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

任何帮助都会很棒! (我对python很新,版本3.5.1)

1 个答案:

答案 0 :(得分:0)

你错过了缩进:

if answer == 'a':

#code is here
sys.exit

应该是:

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

您还需要为其他ifelif执行此操作。

请注意,自()以来添加exit是一个函数,而不是sys的属性。