哪位可以解答下下图这个题目啊?
3个回答
展开全部
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def showList(inventory):
print('inventory:')
item_total=0
for k,v in inventory.items():
print(str(v)+" "+str(k))
item_total=item_total+v
print("Total number of items: "+str(item_total))
# showList(stuff)
def addToInventory(inventory, addedItems):
# your code goes here
for item in addedItems:
inventory.setdefault(item,0)
inventory[item]=inventory[item]+1
return inventory
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv=addToInventory(inv, dragonLoot)
showList(inv)
def showList(inventory):
print('inventory:')
item_total=0
for k,v in inventory.items():
print(str(v)+" "+str(k))
item_total=item_total+v
print("Total number of items: "+str(item_total))
# showList(stuff)
def addToInventory(inventory, addedItems):
# your code goes here
for item in addedItems:
inventory.setdefault(item,0)
inventory[item]=inventory[item]+1
return inventory
inv = {'gold coin': 42, 'rope': 1}
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
inv=addToInventory(inv, dragonLoot)
showList(inv)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
all2={'cherry':5,'pea':6,'apple':2,'water':9}
sub=['bingo','ant','ant']
def invdis(content):
sum2=0
for k,v in content.items():
print(str(v),k)
sum2+=v
print('all2 items qty.',sum2)
def addtoinv(all2,sub):
for data in sub:
if data in all2:
all2[data]+=1
else:
all2[data]=1
return all2
sub=['bingo','ant','ant']
def invdis(content):
sum2=0
for k,v in content.items():
print(str(v),k)
sum2+=v
print('all2 items qty.',sum2)
def addtoinv(all2,sub):
for data in sub:
if data in all2:
all2[data]+=1
else:
all2[data]=1
return all2
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/usr/bin/env python
# coding=utf-8
inv = {'gold coin':42,'rope':1}
dragonLoot = ['gold coin','dagger','gold coin','gold coin','ruby']
def addToInventory(inventory,addedItems):
for i in addedItems:
if inventory.has_key(i):
inventory[i] += 1
else:
inventory[i] = 1
return inventory
inv = addToInventory(inv,dragonLoot)
def displayInventory(inv):
print 'Inventory:'
for k,v in inv.items():
print v,k
displayInventory(inv)
#豆皀楼主,下次记得把题目的代码贴成文字,不然别个还得一个个的照着图片敲
#这题目怎么不然贴代码格式 所以有点乱
------------------------------
Inventory:
45 gold coin
1 rope
1 dagger
1 ruby
# coding=utf-8
inv = {'gold coin':42,'rope':1}
dragonLoot = ['gold coin','dagger','gold coin','gold coin','ruby']
def addToInventory(inventory,addedItems):
for i in addedItems:
if inventory.has_key(i):
inventory[i] += 1
else:
inventory[i] = 1
return inventory
inv = addToInventory(inv,dragonLoot)
def displayInventory(inv):
print 'Inventory:'
for k,v in inv.items():
print v,k
displayInventory(inv)
#豆皀楼主,下次记得把题目的代码贴成文字,不然别个还得一个个的照着图片敲
#这题目怎么不然贴代码格式 所以有点乱
------------------------------
Inventory:
45 gold coin
1 rope
1 dagger
1 ruby
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询