简体   繁体   English

我不明白我的 python 代码有什么问题

[英]I cant understand what's wrong with my python code

The part with the goblin works but the part with the elf doesn't.与妖精的部分有效,但与精灵的部分无效。

#importing the random module
import random

#creating the game object class
class GameObject:
    class_name = ""
    desc = ""
    objects = {}

    def __init__(self, name):
        self.name = name
        GameObject.objects[self.class_name] = self
    
    #defining the description 
    def get_desc(self):
        return self.class_name + "\n" + self.desc

#creating the goblin class
class Goblin(GameObject):
    def __init__(self, name):
        self.class_name = "goblin"
        self.health = 3
        self._desc = "A foul creature"
        super().__init__(name)

    @property
    def desc(self):
        if self.health >= 3:
            return self._desc
        elif self.health == 2:
            x = random.randint(13, 35)
            health_line = "You struck and dealt " + str(x) + " damage!"
        elif self.health == 1:
            y = 40 - random.randint(13, 35)
            health_line = "You rushed and dealt " +str(y) + " damage! \n Goblin activated effect Rage!"
        elif self.health <= 0:
            health_line = "It is dead."
        return self._desc + "\n" + health_line

    @desc.setter
    def desc(self, value):
        self._desc = value
        
#creating the goblin object
goblin = Goblin("Gobbly")
        
#creating the elf class
class Elf(GameObject):
    def __init__(self, name):
        self.class_name = "Elf"
        self.health = 5
        self._desc = "A strong warlock"
        super().__init__(name)
    
    @property
    def desc(self):
        if self.health >= 5:
            return self._desc
        elif self.health == 4:
            x = random.randint(20, 50)
            health_line = " You struck and dealt " + str(x) + " damage!"
        elif self.health == 3:
            x = random.randint(20, 40)
            health_line = " You countered and dealt " + str(x) + " damage!"
        elif self.health == 2:
            y = 40 - random.randint(20, 50)
            health_line = "You rushed and dealt " +str(y) + " damage! \n Elf activated effect Sectum Sempra!!"
        elif self.health == 1:
            y = 40 - random.randint(20, 50)
            health_line = " You struck and dealt " + str(x) + " damage!"
        elif self.health <= 0:
            health_line = "It is dead."
        return self._desc + "\n" + health_line

    @desc.setter
    def desc(self, value):
        self._desc = value

#creating an elf object
elf = Elf("Elfy")

#defining the hit verb
def hit(noun):
    if noun in GameObject.objects:
        thing = GameObject.objects[noun]
        if type(thing) == Goblin:
            thing.health -= 1
            if thing.health <= 0:
                msg = "You killed the goblin!"
            else:
                msg = "You hit the {}".format(thing.class_name)
        elif type(thing) == Elf:
            thing.health -= 1
            if thing.health <= 0:
                msg = "You killed the elf!"
            else:
                msg = "You hit the {}".format(thing.class_name)
    else:
        msg = "There is no {} here.".format(noun)
    return msg

#defining the examine verb
def examine(noun):
    if noun in GameObject.objects:
        return GameObject.objects[noun].get_desc()
    else:
        return "There is no {} here.".format(noun)

#getting input
def get_input():
    command = input(": ").split()
    verb_word = command[0]
    if verb_word in verb_dict:
        verb = verb_dict[verb_word]
    else:
        print("Unknown verb {}".format(verb_word))
        return

    if len(command) >= 2:
        noun_word = command[1]
        print(verb(noun_word))
    else:
        print(verb("nothing"))

#defining the say verb
def say(noun):
    return 'You said "{}"'.format(noun)

#the verbs
verb_dict = {
    "say": say,
    "examine": examine,
    "hit": hit
}

while True:
    get_input()

It's supposed to say |you hit the elf|应该是说|你击中了小精灵| when I type |hit elf|当我输入 |hit elf| 时like how it says |you hit the goblin|就像它说的|你撞到了妖精| when I type |hit goblin|当我输入 |打地精|

I just started learning oop in python and some parts are confusing.我刚开始在 python 中学习 oop,有些部分令人困惑。 If anyone understands, please help me fix the code.如果有人理解,请帮我修复代码。

At first elif refers to else if , that simply means that if statement is activated then the elif statement gets skipped .起初 elif 指的是 else if ,这只是意味着 if 语句被激活然后 elif 语句被跳过。 So try replacing elif with if .所以尝试用 if 替换 elif 。 If problem still continues , reply.如果问题仍然存在,请回复。

I don't see that you need to check the type of the object here:我不认为您需要在此处检查对象的类型:

def hit(noun):
    if noun in GameObject.objects:
        thing = GameObject.objects[noun]
        thing.health -= 1
        if thing.health <= 0:
            msg = "You killed the {}!".format(thing.class_name.lower())
        else:
            msg = "You hit the {}".format(thing.class_name)
    else:
        msg = "There is no {} here.".format(noun)
    return msg

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM