繁体   English   中英

列表未出现且 elif 语句未激活

[英]List doesn't appear and elif statement doesn't activate

在我的文本RPG中,我的攻击功能设置如下

def attack(self):

    spell = [magic_spell for magic_spell in self.spell_book if isinstance(magic_spell,magic.Spell)]
    if not spell:
        print("You have no spells to cast!")
    
    input('What do you want to attack with? Melee or Magic: ')

    if input == str('magic'):
        for i, magic_spell in enumerate(spell, 1):
            print('Choose a spell to cast:')
            print(f"{i}. {spell}")
        
        valid =False
        while not valid:
            choice = input("")
            try:
                if player.mana == 0:
                    print("You dont have enough mana")
                else:   
                    room = world.tile_at(self.x,self.y)
                    enemy = room.enemy
                    print("You use {} against {}!".format(spell,enemy.name))
                    enemy.hp -=  spell.damage
                    self.mana = self.mana - spell.mana

                    if not enemy.is_alive():
                        print("You killed {}!".format(enemy.name))
                        
                    else:
                        print("{} HP is {}.".format(enemy.name,enemy.hp))
            except(ValueError,IndexError):
                print("Invalid choice, try again")      
    elif input == str('melee'):
        best_weapon = self.most_powerful_weapon()
        room = world.tile_at(self.x,self.y)
        enemy = room.enemy
        print("You use {} against {}!".format(best_weapon.name,enemy.name))
        enemy.hp -=  best_weapon.damage

        if not enemy.is_alive():
            print("You killed {}!".format(enemy.name))
            
        else:
            print("{} HP is {}.".format(enemy.name,enemy.hp))

当您在提示中键入magic时,函数的列表拼写部分不会出现。 列表法术功能直接模仿我的heal功能,但当然不是治疗,而是使用选定的法术造成伤害。

def heal(self):
    consumables = [item for item in self.inventory if isinstance(item,items.Consumables)]
    if not consumables:
        print("You dont have any items to heal you!")
        return 

    for i, item in enumerate(consumables, 1):
        print("Choose an item to use to heal: ")
        print("{}. {}".format(i, item))
    
    valid = False
    while not valid:
        choice = input("")
        try:
            to_eat = consumables[int(choice)-1]
            self.hp = min(100,self.hp + to_eat.healing_value)
            self.inventory.remove(to_eat)
            print("Current HP: {}".format(self.hp))
            valid = True
        except (ValueError,IndexError):
            print("Invalid choice, try again")

我很困惑为什么heal功能会起作用,但attack却不起作用。 进入近战也不会执行近战攻击。

input是一个内置函数,不建议像变量一样使用它。

但是您代码中的主要错误是您没有将输入存储在变量中。

用这个:

user_input = input("What do you want to attack with? Melee or Magic: ")

暂无
暂无

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

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