2017-10-21 51 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

相关问题