【问题标题】:Python "Maximum Recursion Depth Exceeded Error"Python“最大递归深度超出错误”
【发布时间】:2021-12-11 07:57:50
【问题描述】:
    def updatedcollisions(self):
        self.xcord, self.ycord = pygame.mouse.get_pos()
        if self.confirmationscreen == True:
            if self.xcord < 150 or self.xcord > 650 and self.ycord < 200 or self.ycord > 700:
                print('Out of Screen')
                self.confirmationscreen = False
                pygame.Surface.fill(self.win,('black'))
                self.loadonce()
                self.buttons()
                self.font = pygame.font.Font(r'D:\Games\First Game\FreeSans\FreeSansBold.ttf',28)
                self.text = self.font.render(f'Multiplier {self.moneyperclick}', True, (255,255,255))
                self.textRect = self.text.get_rect()
                self.textRect.center = (self.width//2, 150)
                pygame.draw.rect(self.win,(0,0,0),self.textRect)
                self.win.blit(self.text, self.textRect)
        else:
            if 300 <= self.xcord <= 400 and 600 <= self.ycord <= 700:
                self.win.blit(self.whiteboard,[(self.width/2)-150,(self.height/2)-200])
                self.multiplier()
                self.confirmation()
            elif 150 <=self.xcord <= 250 and 600 <= self.ycord <=700:
                print('Worked')
                self.money = self.money - self.moneyperclick
                pygame.display.update()
            else:
                self.whiteboardfiller()
                self.updatedcollisions()
                self.moneytracker()
                self.money = round(self.money,2)
                self.changingtextstuff(f'Whiteboards {self.money}')

我做了一些研究,发现这意味着它正在循环某些东西,但我不明白为什么它无法处理这个。错误消息还显示if type(random) is BuiltInMethod or type(getrandbits) is Method:。如果有人可以提供帮助,那就太棒了。 完整代码是here

【问题讨论】:

  • self.updatedcollisions() - 方法调用自身。在这个方法中似乎没有任何东西会改变任何变量,因此条件不会再次相同,所以它会再次调用自己,以此类推。

标签: python python-3.x recursion


【解决方案1】:

超过最大递归深度意味着您的代码调用自身的次数过多,解释器无法处理。在这种情况下,我认为罪魁祸首是您的第二个 else 语句中的 self.updatedcollisions()

假设您在代码中调用了updatedcollisions()。如果confirmationscreen 为假,则跳转到第一个 else 块。在那个 else 块中,如果这个对象不在正确的范围内(if 和 elif 语句都是假的),代码会跳转到最后的 else 块。在最后一个 else 块中,updatedcollisions() 再次被调用,循环不断重复,直到递归太深而无法处理,导致程序崩溃。

【讨论】:

    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2017-07-18
    相关资源
    最近更新 更多