【问题标题】:I cant understand what's wrong with my python code我无法理解我的 python 代码有什么问题
【发布时间】:2020-10-30 07:47:56
【问题描述】:

妖精部分有效,而精灵部分无效。

#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()

应该是说|你击中了精灵|当我输入 |hit elf|就像它所说的|你击中了妖精|当我输入 |hit goblin|

我刚开始在 python 中学习 oop,有些部分令人困惑。如果有人明白,请帮我修复代码。

【问题讨论】:

  • 这只是一个错字:“Elf”有一个大写E。稍微调试一下就很容易找到。只需print(GameObject.objects),问题就会立即显现出来。

标签: python python-3.x class oop object


【解决方案1】:

最初 elif 指的是 else if ,这仅仅意味着 if 语句被激活然后 elif 语句被跳过。所以尝试用 if 替换 elif 。如果问题仍然存在,请回复。

【讨论】:

    【解决方案2】:

    我看你这里不需要检查对象的类型:

    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
    

    【讨论】:

    • 我还能怎么做?
    • 刚刚发现我忘了删除类型检查的 if 行,这个函数应该和你的 hit 函数一样,但不检查对象是妖精还是精灵
    • 我删除了 if 语句并相应地更改了缩进,但它仍然不起作用
    • 如果你能准确地展示你的代码是如何不起作用的(当精灵被击中时会发生什么,当地精被击中时会发生什么?你看到什么错误,如果有的话?)如果你的代码正常工作(即没有错误),但没有做你想做的事,这是另一种问题
    • 当我输入 hit goblin 时它说你击中了 goblin 但是当我输入 hit elf 时它说这里没有精灵
    猜你喜欢
    • 2014-04-18
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多