调用另一个函数错误

时间:2017-10-21 16:26:43

标签: python function

这应该是我可以练习python的测试程序。我定义了main并构建了代码。当答案是对的时候,我按下“Y'并且它应该跳转到下一个函数,这个函数在此结束之后是下一个代码块。错误是get:

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 个答案:

答案 0 :(得分:1)

在main()

的定义之前移动logic_ques()的定义

答案 1 :(得分:0)

是的,问题在于订单。在定义之前,您正在调用main()中的logic_ques()函数。只需将logic_ques()的定义移到您调用main()

的点之上