2017-10-21 86 views
1

這應該是一個測試程序,我可以在其上練習python。我定義了main並構建了代碼。當答案是正確的時候,我按下'Y',它應該跳到下一個代碼塊的下一個函數,在這個函數結束之後。錯誤是這樣的:調用另一個函數錯誤

NameError: name 'logic_ques' is not defined. 

如何在按'y'並且沒有出現錯誤後啓動下一個功能?問題是順序嗎?

def main(): 

    pts = 0 
    numberStr = input("Please enter the sum of 251 and 516: \n ") 
    num = int(numberStr) 
    print ('You have entered: ', num) 

    if (num == 767): 
     pts += 1 
     print ('The answer is correct!') 
     print ('You currently have ', pts, 'point(s).') 
     continue1 = input('Press any key to see the next question.') 
     logic_ques() 
    else: 
     print ('The answer is not correct.') 
     restart = input('The answer is wrong, type Y if you want to restart, and N if you want to exit. \n') 
     if (restart == 'y'): 
      main() 
     else: 
      exit() 

main() 

def logic_ques(): 

    logicStr = input("Which animal sleeps on legs, not lying down?") 
    print ('You have entered the following animal:', logicStr) 

    if (logicStr == 'horse'): 
     pts += 1 
     print ('The asnwer is correct!') 
     print ('You currently have ', pts, 'points.') 
     continue1 = input('Press any ket to see the next question.\n') 
    else: 
     print ('The asnwer is not correct!') 
     restart1 = input('The answer is wrong, type Y if you want to restart, and N if you want to exit. \n') 
     if (restart1 == 'y'): 
      logic_ques() 
     else: 
      exit() 
logic_ques() 
+2

你叫'main'的定義之前移動logic_ques()的定義**之前調用點上方**到達點在你定義'logic_ques'的腳本中。是的,問題在於訂單。 – jonrsharpe

回答

0

是的,問題是順序。在定義它之前,您正在調用main()中的logic_ques()函數。只需移動)logic_ques的定義(在這裏你的main()

+0

問題是,如果答案是錯誤的,我想返回到它將返回到我定義logic_ques()並開始腳本的開始位置的問題,所以基本上程序結束了。 –

+0

如果你想繼續向用戶提問這些問題,直到他們弄錯爲止,我會使用一個循環,不斷詢問問題,直到它們正確或輸入「N」,而不是遞歸地調用該函數。除此之外,我不確定問題是什麼。 – divibisan

相關問題