Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python变量未按预期工作_Python_Variables_Python 3.3 - Fatal编程技术网

Python变量未按预期工作

Python变量未按预期工作,python,variables,python-3.3,Python,Variables,Python 3.3,我在顶部将伤害设置为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": pri

我在顶部将伤害设置为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,但当我键入stats时,它将显示damage=3,您可以读取全局变量,但如果您希望分配(重新绑定)给它们,则需要告诉Python您打算使用
全局
关键字

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

您应该将这些属性移动到类中。在这个例子中,我称它为
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""")

damage
不是一个
global
变量,
main
有它自己的作用域……修复@zondo建议不会处理您的结构问题。为什么不为用户/玩家状态创建另一个类似于敌方类的类,这样它就不会扰乱全局名称空间?如果你不明白我的建议,我可以把它写在一个答案里。