【问题标题】:Python Recursion Error maximum recursion depth exceeded while calling Python object调用 Python 对象时超出 Python 递归错误最大递归深度
【发布时间】:2016-03-09 22:36:37
【问题描述】:

我有一个用 Python 编写的小程序。除了基于命令行之外,它应该像 Siri 一样与您交谈。它会问你今天过得怎么样,当你说得好时,它应该从 5 个好的回答中选出 1 个对你说,但我不断收到递归错误,这是我的代码

def nice5():
    print ("Well Thats Good!")
   time.sleep(2)
   randsub(2)

def nice4():
    print ("Great!")
    time.sleep(2)
    randsub()

def nice3():
    print ("Thats Good!")
    time.sleep(2)
    randsub()

def nice2():
    print ("Cool I am glad to hear that!")
    time.sleep(2)
    randsub()

def nice1():
    print ("Thats Good! ")
    time.sleep(2)
    randsub()

def nicerand():
    randnices = [nice1 , nice2 , nice3 , nice4 , nice5]
    randnice = random.choice(randnices)
    nicerand()


def p1checkChoice():
    if choice == "Good":
        nicerand()
    if choice == "good":
       nicerand()
    if choice == "GOOD":
       nicerand()




def greeting():
    clearscr()
    name = input("Hello What is your name: ")
    clearscr()
    print ("Hello " + name)
    time.sleep(2)
    clearscr()
    print ("Allow me to introduce myself")
    time.sleep(2)
    clearscr()
    print ("My name is Chatbot")
    time.sleep(2)
    clearscr()
    choice = input("How has your day been: ")
    global choice
    p1checkChoice()
    greeting()

【问题讨论】:

  • 能否提供准确的错误信息和Randnices的定义
  • RecursionError: 调用 Python 对象时超出最大递归深度
  • Global choice 大写 G?请提供您使用的确切来源。

标签: python-3.x


【解决方案1】:

您的示例代码中有太多错误,而不是您正在运行的实际代码;但是,关键问题是checkChoice 调用nicerand 调用任何randnices 调用nicerand 调用任何randnices 调用nicerand 调用任何randnices 调用....

最终你点击了RecursionError

解决方法很简单:没有任何randnices 函数回调到nicerand;只需从他们那里删除该行。

然后他们将简单地返回到nicerand,这将返回到checkChoice,它可以继续它正在做的任何事情。

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 2021-10-05
    • 1970-01-01
    • 2019-04-27
    • 2017-11-07
    • 2011-10-12
    • 2016-01-25
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多