Python 变量未按预期工作

Python variable not working as expected

我在顶部将伤害设置为 3,但是当我在底部键入伤害 += 3 时,它说未引用伤害,为什么会这样?

import random, time


inventory = ['dagger','torch']
hp = 50
maxhp = 50
damage = 3
ac = 4
gold = 10
in_combat = False

def choice():
    choose = input(">> ")
    if choose == "stats":
        print("Health:",hp,", Damage:",damage,", Armour: ",ac,", Gold:",gold)
elif choose == "backpack":
    print(inventory)
elif choose == "help":
    print("Keywords:\nstats | view your stats\nbackpack | view your inventory\nhelp | view keywords to input\nattack | attack an enemy when in combat")
else:
    print("Invalid Input")




class Enemy:
    def __init__(self, name, attack, armour, health):
        self.name = name
        self.attack = attack
        self.armour = armour
        self.health = health

    def attack_enemy(self):
        time.sleep(1)
        print("What action do you want to make?\nType 'help' for a list of actions\n")
        answer = input(">> ")
        if answer == "attack":
            self.health = self.health - damage
            print(self.name,"health is: ",self.health)



def main():
    while hp > 0:
        if 'dagger' in inventory:
            damage += 3
        print(damage)
        choice()

main()

此外,如果我将底部的代码更改为 dagger = 6,它会打印 6,但当我输入统计信息时,它会显示 damage = 3

您可以读取全局变量,但是如果您希望将它们赋值(重新绑定),您需要告诉Python您的意思是使用global关键字。

在这种情况下,根本不需要将这些变量设为全局变量。

您应该将这些属性移动到 class 中。在这个例子中我称之为 Player

import random, time


class Player:
    def __init__(self):
        self.inventory = ['dagger','torch']
        self.hp = 50
        self.maxhp = 50
        self.damage = 3
        self.ac = 4
        self.gold = 10
        self.in_combat = False

    def choice(self):
        choose = input(">> ")
        if choose == "stats":
            print("Health:", self.hp, ", Damage:", self.damage,
                    ", Armour:", self.ac, ", Gold:", self.gold)
        elif choose == "backpack":
            print(inventory)
        elif choose == "help":
            print("Keywords:\nstats | view your stats\nbackpack | view your inventory\nhelp | view keywords to input\nattack | attack an enemy when in combat")
        else:
            print("Invalid Input")




class Enemy:
    def __init__(self, name, attack, armour, health):
        self.name = name
        self.attack = attack
        self.armour = armour
        self.health = health

    def attack_enemy(self):
        time.sleep(1)
        print("What action do you want to make?\nType 'help' for a list of actions\n")
        answer = input(">> ")
        if answer == "attack":
            self.health = self.health - damage
            print(self.name,"health is: ",self.health)



def main():
    pl = Player()
    while pl.hp > 0:
        if 'dagger' in pl.inventory:
            pl.damage += 3
        print(pl.damage)
        pl.choice()

main()

旁白:由于您可能要打印大量多行块,请查看 textwrap 中的 dedent 函数。你可以像这样使用它:

from textwrap import dedent

def deprint(s):
    print(dedent(s))

...


        elif choose == "help":
            deprint("""
                 Keywords:
                 stats | view your stats
                 backpack | view your inventory
                 help | view keywords to input
                 attack | attack an enemy when in combat""")