简体   繁体   English

AttributeError:'NoneType'对象没有属性'name

[英]AttributeError: 'NoneType' object has no attribute 'name

I'm new to python and programming. 我是python和编程的新手。 Im trying to create a simple (for now) text game and have a problem. 我试图创建一个简单的(现在)文本游戏,但遇到了问题。 Here's part of my code I have problem with 这是我遇到的部分代码

 class Monster:
         def __init__(self,name,hp,ac,exp,thaco):
            self.name=name
            self.hp=hp
            self.ac=ac
            self.exp=exp
            self.thaco=thaco


    class Zombie(Monster):
        def __init__(self):
            super().__init__(name="Zombie",
                             hp=10,ac=5,
                             exp=1,thaco=20)

        POWER=[1,2,3,4,5,6,7]

    class Ghul(Monster):
        def __init__(self):
            super().__init__(name="Ghul",
                             hp=12,ac=6,
                             exp=1,thaco=20)

        POWER=[1,2,3,4,5,6]

    class Skeleton(Monster):
        def __init__(self):
            super().__init__(name="Skeleton",
                             hp=6,ac=2,
                             exp=1,thaco=20)

        POWER=[1,2,3,4]

    class Ghost(Monster):
        def __init__(self):
            super().__init__(name="Ghost",
                             hp=5,ac=10,
                             exp=2,thaco=20)

        POWER=[1,2,3,4,5,6]


    class Slime(Monster):
        def __init__(self):
            super().__init__(name="Slime",
                             hp=26,ac=8,
                             exp=4,thaco=20)

        POWER=[5,6,7,8,9,10]


    def random_mob():
            while twenty_sided_die.roll() <=5 :
                mob=Zombie()
                return mob
            while 5 < twenty_sided_die.roll() <= 10:
                mob=Ghul()
                return mob
            while 10 < twenty_sided_die.roll() <= 15:
                mob=Skeleton()
                return mob
            while 15 < twenty_sided_die.roll() <= 19:
                mob=Ghost()
                return mob
            while twenty_sided_die.roll() > 19:
                mob=Slime()
                return mob


        mob = random_mob()
for command, action in hero.COMMANDS.items():
    print("Press {} to {}".format(command, action[0]))
while True:
    command = input("~~~~~~~Press key to continue~~~~~~~")
    if command not in hero.COMMANDS:
        print("Not a valid command")
        continue
    print("You are fighting "  + mob.name)
    time.sleep(1)
    print("")
    break

Problem is at the last part of the code when printing mob to fight. 问题出在打印暴民时代码的最后部分。 Every few tries, I got error: 每尝试几次,我都会出错:

AttributeError: 'NoneType' object has no attribute 'name and I cannot find any reason why. AttributeError:'NoneType'对象没有属性'name,我找不到任何原因。

Aprreciate for any advice 感谢任何建议

The error comes from you random_mob function. 该错误来自您的random_mob函数。 Try this: 尝试这个:

def random_mob():
        roll = twenty_sided_die.roll()
        if roll <= 5 :
            return Zombie()
        elif roll <= 10:
            return Ghul()
        elif roll <= 15:
            return Skeleton()
        elif roll <= 19:
            return Ghost()
        else:
            return Slime()

Explanation: you should only roll your die once, store the result and test it against all sub-ranges. 说明:您应该只滚动一次模具,存储结果并针对所有子范围进行测试。 In your original function, you roll the die several times and you've got a chance that all tests return False, which means that the function returns None 在您的原始函数中,您掷骰数次,并且所有测试都有可能返回False,这意味着该函数返回None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;NoneType&#39;对象没有属性&#39;name&#39; - AttributeError: 'NoneType' object has no attribute 'name' Flask AttributeError: 'NoneType' object 没有属性 'name' - Flask AttributeError: 'NoneType' object has no attribute 'name' AttributeError: 'NoneType' object 没有带有 streamlit 的属性 'name' - AttributeError: 'NoneType' object has no attribute 'name' with streamlit AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;name&#39;? - AttributeError: 'NoneType' object has no attribute 'name'? Tkinter: AttributeError: NoneType object 没有属性<attribute name></attribute> - Tkinter: AttributeError: NoneType object has no attribute <attribute name> AttributeError: &#39;NoneType&#39; 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;name&#39;(与不同的 tensorflow 版本有关) - AttributeError: 'NoneType' object has no attribute 'name' ( related to different tensorflow versions ) django-AttributeError:“ NoneType”对象没有属性“ first_name” - django - AttributeError: 'NoneType' object has no attribute 'first_name' AttributeError:在SST Python中执行时,“ NoneType”对象没有属性“名称” - AttributeError: 'NoneType' object has no attribute 'name' while executing in SST Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM