簡體   English   中英

我不明白我的 python 代碼有什么問題

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

與妖精的部分有效,但與精靈的部分無效。

#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| 時就像它說的|你撞到了妖精| 當我輸入 |打地精|

我剛開始在 python 中學習 oop,有些部分令人困惑。 如果有人理解,請幫我修復代碼。

起初 elif 指的是 else if ,這只是意味着 if 語句被激活然后 elif 語句被跳過。 所以嘗試用 if 替換 elif 。 如果問題仍然存在,請回復。

我不認為您需要在此處檢查對象的類型:

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