2013-09-29 20 views
3

Im新的python和一個類。以下是我的代碼。我的問題是在我想要的最後一行,所以如果有人簡單地按下輸入,它會計數,就好像有人做了除(1/2/3或4)之外的輸入,它基本上會回覆最後一個elif並重新啓動問題。但目前它給了我這個:如何計數空輸入作爲其他

SyntaxError: unexpected EOF while parsing. 

任何人都可以幫忙嗎?

def menu_loop(): 
    ans = True 
    while ans: 
     print(""" 
     1. My freedom awaits me, give me my blade! 
     2. I've never fought before, might you help me? 
     3. I have already forsaken freedom, I will fight till the death. 
     4. Fighting is for the wicked. I refuse to fight. 

     Press enter to quit 
     """) 

     ans= input("What would you like to do now?") 
     if ans == 1: 
      main_loop() 
      break 
     elif ans == 2: 
      instructions_loop() 
      break 
     elif ans == 3: 
      gauntlet_loop() 
      break 
     elif ans == 4: 
      print("\nMen... come relieve this man of his life.") 
      time.sleep(2) 
      end_game_loop() 
      break 
     elif ans == '': 
      print("\nDo you not speak Latin, slave?") 

謝謝你,我找到了答案感謝基督徒。我猜我從Python 3得到的指示,我在2(這是一個大學作業)。它現在正在工作,非常感謝!

回答

0

如果你使用python 2然後改變:

ans = input("What would you like to do now?") 

到:

ans = raw_input("What would you like to do now?") 

raw_input返回一個字符串,而輸入返回integer這是在Python 3改變,3只存在input,它返回一個字符串

所以如果你使用python 2將其更改爲raw_input然後ca LL你的其他條件是這樣的:

if int(ans) == 1: 
    main_loop() 
    break 
+0

謝謝你,這個幫了不少忙。我將輸入改爲raw_input,因爲我的確在使用python 2(.7)。當我添加int部分時,事情發生了,所以我只是把數字放在引號內,現在一切都很順利。非常感謝:D – GOAT

+0

是的,你也可以做到這一點,我很高興我可以幫助你:)與你的其他項目祝你好運! – Serial

0

改變最後的elif條件elif not ans

def menu_loop(): 
    ans = True 
    while ans: 
     print(""" 
     1. My freedom awaits me, give me my blade! 
     2. I've never fought before, might you help me? 
     3. I have already forsaken freedom, I will fight till the death. 
     4. Fighting is for the wicked. I refuse to fight. 

     Press enter to quit 
     """) 

     ans = input("What would you like to do now?") 
     if ans == 1: 
      main_loop() 
      break 
     elif ans == 2: 
      instructions_loop() 
      break 
     elif ans == 3: 
      gauntlet_loop() 
      break 
     elif ans == 4: 
      print("\nMen... come relieve this man of his life.") 
      time.sleep(2) 
      end_game_loop() 
      break 
     elif not ans: 
      print("\nDo you not speak Latin, slave?") 

編輯:答案是蟒蛇3 對於Python 2.7,使用的的raw_input而不是輸入,並相應地使用int(ans)或字符串文字。

+0

你在Python 2或Python 3上測試過嗎? –

+0

@RayToal蟒蛇3. 關於Python 2,你需要將'input'功能改成'raw_input',和'ans'然後改到'INT(ANS)' –

+0

我的是蟒蛇2,但由於反正! – GOAT

0

使用raw_input代替inputinput將嘗試評估輸入,評估空字符串會產生錯誤。 raw_input不評估,只是給你一個字符串。請參閱this explanation

def main_loop(): 
    pass 
def instructions_loop(): 
    pass 
def gauntlet_loop(): 
    pass 
def end_game_loop(): 
    pass 

next_loop_func = { 
    '1': main_loop, 
    '2': instructions_loop, 
    '3': gauntlet_loop, 
    '4': end_game_loop, 
} 
def menu_loop(): 
    while True: 
     print(""" 
     1. My freedom awaits me, give me my blade! 
     2. I've never fought before, might you help me? 
     3. I have already forsaken freedom, I will fight till the death. 
     4. Fighting is for the wicked. I refuse to fight. 

     Press enter to quit 
     """) 

     ans = raw_input("What would you like to do now?") 
     if ans in next_loop_func: 
      next_loop_func[ans] 
     else: 
      print("\nDo you not speak Latin, slave?") 


menu_loop() 
相關問題