用python编写保存和返回值的函数
我目前正在试验Python并编写一些文本冒险。在我的游戏中,玩家拥有某些属性,如hp、攻击伤害和物品库存。 我希望能够从代码中的任何地方调用这些属性。为此,我创建了一个接收三个值的函数: “编辑”:指定是否应编辑变量 “info_id”:指定应访问哪个变量 “值”:变量的新值 在我的代码中是这样的:用python编写保存和返回值的函数,python,function,variables,save,call,Python,Function,Variables,Save,Call,我目前正在试验Python并编写一些文本冒险。在我的游戏中,玩家拥有某些属性,如hp、攻击伤害和物品库存。 我希望能够从代码中的任何地方调用这些属性。为此,我创建了一个接收三个值的函数: “编辑”:指定是否应编辑变量 “info_id”:指定应访问哪个变量 “值”:变量的新值 在我的代码中是这样的: def player_info(edit, info_id, value): if edit == 1: ##function wants to edit value if i
def player_info(edit, info_id, value):
if edit == 1:
##function wants to edit value
if info_id == 1:
player_hp = value
print ("Assigned hp to: ", player_hp)
##the "prints" are just to check if the asignments work -> they do
return player_hp
elif info_id == 2:
player_attack = value
print ("Assigned attack to: ", player_attack)
return player_attack
elif info_id == 3:
item_1 = value
return item_1
elif info_id == 4:
item_2 = value
return item_2
elif info_id == 5:
item_3 = value
elif edit == 0:
##function wants to retrieve value
if info_id == 1:
return player_hp
elif info_id == 2:
return player_attack
elif info_id == 3:
return item_1
elif info_id == 4:
return item_2
elif info_id == 5:
return item_3
实际上有10个项目槽(上升到info_id==13),但它们都是相同的
我在代码开头定义了所有变量:
player_info(1,1,20)
player_info(1,2,5)
n=3
while n<=13:
player_info(1,n,0)
n=n+1
##items are not fully implemented yet so I define the item slots as 0
我得到一个错误:
local variable 'player_hp' referenced before assignment
函数是否未正确保存变量?或者问题出在哪里
是否有更好的方法保存变量?在这种情况下,全局变量是可行的吗?
谢谢你的帮助 首先,您的错误是由于检索一个未赋值的变量而导致的,这根本不起作用。当您编辑hp播放器时,它不会存储在任何地方。您将其返回给调用它的函数,而不是将其分配给任何对象。它只是迷路了 第二,你真的应该用4个空格(或制表符)缩进——它比2个空格更可读。不仅是为了你,也为了任何想帮忙的人 最后,正确的方法是学习课程。在python中永远不要使用全局变量,只能在特殊情况下使用,或者在学习时使用,只需跳到前面的类即可 您应该创建类似于
class Player:
def __init__(self):
self.hp = 20 # or another starting hp
self.attack = 3 # or another starting attack
self.inventory = []
然后您可以创建一个Player类的实例,并将其传递给相关的函数
player1 = Player()
print(player1.hp) # Prints out player's hp
player1.hp -= 5 # Remove 5 hp from the player. Tip: Use method to do this so that it can check if it reaches 0 or max etc.
player1.inventory.append("axe")
print(player1.inventory[0]) # Prints out axe, learn about lists, or use dictionary, or another class if you want this not to be indexed like a list
您询问,“函数是否未正确保存变量?”
通常,Python函数不保存其状态。使用yield
语句的函数除外。如果你写一个这样的函数
def save_data(data):
storage = data
save_data(10)
这样称呼它
def save_data(data):
storage = data
save_data(10)
以后将无法获取存储的值。在Python中,如果需要保存数据并在以后检索数据,通常会使用
Pythonclasses
允许您执行以下操作:
player_info(0,1,0)
class PlayerData(object):
def __init__(self, hp=0, damage=0):
self.hp = hp
self.damage = damage
self.inventory = list()
self.max_inventory = 10
def add_item(self, item):
if len(self.inventory) < self.max_inventory:
self.inventory.append(item)
def hit(self, damage):
self.hp -= damage
if self.hp < 0:
self.hp = 0
def attack(self, other):
other.hit(self.damage)
if __name__ == '__main__':
player1 = PlayerData(20, 5)
player2 = PlayerData(20, 5)
player1.attack(player2)
print player2.hp
player1.add_item('sword')
player1.add_item('shield')
print player1.inventory
这实际上只是触及了如何使用类的表面。在更完整的实现中,您可能有一个项
基类。然后您可以创建继承自项的剑
和盾
类请格式化您的代码。缩进是函数创建名为player\u hp
等的局部变量时的一种信息,它不会修改同名的全局变量。您可以使用global
关键字使函数修改globals,但是最好创建一个Player类,并使用该类的实例来存储Player信息。感谢这些伟大的示例!我将更深入地研究课程,它们太棒了!