为什么要返回一个none
def addtoinventory(a,b):#返回一个字典for i in b:
k = a.get(i,0)#get方法取得列表对应的键的值
k = k + 1
a = k#修改了原来的字典
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addtoinventory(inv,dragonLoot)
print(inv)
为什么这里的字典被返回一个none def addtoinventory(a,b)函数没有返回值啊
在调用的时候inv = addtoinventory(inv,dragonLoot)
当然值为none 你试试这样:
def addtoinventory(a,b):#返回一个字典
for i in b:
k = a.get(i,0)#get方法取得列表对应的键的值
k = k + 1
a = k#修改了原来的字典
return a
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv = addtoinventory(inv,dragonLoot)
print(inv)
页:
[1]