如何修复我的" MemoryError:StackOverflow"?

时间:2017-09-14 18:24:23

标签: python recursion

我可以缩短或以其他方式更改程序以消除错误吗?

#This is Where Everything about the Player is stored
class Player_Data:
    def Player_Data_Vars(self):
        import sys
        #print(sys.getrecursionlimit())
        sys.setrecursionlimit()
        #print(sys.getrecursionlimit())
        def Xp(self):

            Level = 0
        Xp = 0
        Xp = Xp + 10
        if Xp == 10:
            Xp = 0
            Level = Level + 1
            if Xp == 0:
                Hp(self)
        return
        def Hp(self):
            Level = Xp('Level')
            Base_Hp = 100
            Level_Up_Hp = + 20 * Level
            Base_Hp = Base_Hp + Level_Up_Hp
            print(Base_Hp)
            return
        def Atk(self):
            Fists = 10
            Stick = 10
            Weopon = Fists
            Wand = Stick

            Level = 0
            Level_Up_Magic_Dmg = + 10 * Level
            Level_Up_Dmg = + 10 * Level
            Base_Player_Dmg = 10 + Weopon + Level_Up_Dmg
            Base_Magic_Dmg = 10 + Wand + Level
            print(Base_Magic_Dmg, Base_Player_Dmg)
            return

    Xp('self')
    print('')
    Atk('self')
    print('')
    Hp('self')

Player_Data_Vars('self')


Player_Data = Player_Data

我的结果:

Traceback (most recent call last):
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
    Hp(self)
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 20, in Hp
    Level = Xp('Level')
  File "C:/Users/Luke/Desktop/Game Project Folder Python/Player Data.py", line 17, in Xp
MemoryError: Stack overflow
repeated:

1 个答案:

答案 0 :(得分:2)

<强>分析

基本问题是您在类设计中内置了直接的无限递归。 Xp 经历了一些奇怪的阴谋,迫使局部变量从0到10再回到0,然后调用 Hp Hp 然后立即使用未使用的参数调用 Xp ,然后重新开始。

<强>症状

您的编码表明您还没有学会使用字符串(例如“自我&vs; 自我”),本地与类变量(您似乎认为 Level 将神奇地出现在您定义它的方法之外),基本数据流(与您对变量 Xp 的操作一样),类和方法结构,以及各种其他语言基础知识。

从何处开始

使用增量编程:从 tiny 任务开始,例如设置玩家的初始特征 - 不是一个类,只有一个玩家作为全局变量。写下几行代码。不要继续,直到他们工作:你已经合理地测试了它们。

然后添加另一个小功能。使初始化成为一个函数。从主程序(远左边距)调用它并测试它。

现在写一个简单的升级功能。然后是一个琐碎的攻击功能。确保您知道如何从主程序中调用它们。

这样,当您遇到问题时,您总会知道它可能出现在您编写的最后几行中。此外,您将一次学习一两件事,而不是十几件。在这里,你超过了自己并且在不到40行的结中打成了自己。