``````"京巴":30,
"藏獒":45,
"二哈":15,
``````

}

def dog(name,d_type):

``````data = {
"name":name,
"d_type":d_type,
#"attack_val":d_type,
"life_val":100
}
if d_type in attack_vals:
data["attack_val"] = attack_vals[d_type]
else:
data["attack_val"] = 15

def dog_bite(person_obj):
person_obj["life_val"] -= data["attack_val"]
print("狗[%s]咬了人[%s]一口，人掉血[%s],人还剩血量[%s]..." % (data["name"],
person_obj["name"],
data["attack_val"],
person_obj["life_val"]))
data["bite"] = dog_bite
return data
``````

def person(name,age):
data = {
"name":name,
"age":age,
"life_val":100
}
if age > 18:
data["attack_val"] = 35
else:
data["attack_val"] = 15
return data

``````def person_bate(dog_obj):
dog_obj["life_val"] -= person_obj["attack_val"]
print("人[%s]打了狗[%s]一棒，狗掉血[%s],狗还剩血量[%s]..." % (data["name"],
dog_obj["name"],
data["attack_val"],
dog_obj["life_val"]))
data["beat"] = person_bate
return data
``````

P1 = person("Laiyfe",25)
d1 = dog("MJJ","二哈")

P1"beat"

``````**Traceback (most recent call last):
File "F:/Code/2019.12.12/venv/2.py", line 55, in <module>
P1["beat"](d1)
KeyError: 'beat'**

``````

1个回答

'ゞBibo。 回复Python_laiyfe: 对啊，P1里都没有beat啊，P1["beat"]当然报错了
10 个月之前 回复
Python_laiyfe 而且上面bite是同样的代码可以执行，为什么下面这个bate就不能执行了呢
10 个月之前 回复
Python_laiyfe {'name': 'Laiyfe', 'age': 25, 'life_val': 100, 'attack_val': 35} P1打印结果是这个
10 个月之前 回复