下面是一个练习- 幻想游戏库存 -
你正在创造一个幻想的电子游戏。为玩家的库存建模的数据结构将是一个字典,其中键是描述库存中项目的字符串值,值是一个整数值,详细说明了该播放器有多少项。例如,字典值
{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}表示玩家有1根绳子、6根手电筒、42枚金币等等。编写一个名为display_inventory()的函数,它将接受任何可能的“库存”,并显示如下所示:清单: 12支箭、42枚金币、1根绳子、6支火把、1把匕首、62支提示--你可以使用for循环循环遍历字典中的所有键。
我写了以下代码。任何反馈都是非常感谢的。
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for item in inventory:
print(str(inventory[item]) + ' ' + item)
total_items += inventory[item]
print("Total number of items: " + str(total_items))
if __name__ == '__main__':
display_inventory(stuff)发布于 2019-06-15 15:48:56
我建议使用fstrings和字典items()方法。
这个
print(f'{value} {key}')而不是
print(str(inventory[item]) + ' ' + item)更整洁:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')此外,还可以通过sum()函数和字典values()方法计算所需位置的总数。然后,您不需要total_items变量。
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')发布于 2019-06-15 18:03:18
正如罗兰·伊利格在评论中提到的,我忽略了从奇数生成正确复数形式的有趣部分。
下面是一个支持Python3-屈折的模块。
# Initialization
import inflect
p = inflect.engine()word = "torch"
print(f"The plural of '{word}' is '{p.plural(word)}'.")>>> The plural of 'torch' is 'torches'.word = "torches"
print(f"The singular of '{word}' is '{p.singular_noun(word)}'.")>>> The singular of 'torches' is 'torch'.我在迷你麦克斯答案上展开的更新代码是:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value != 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)这将提供以下输出-
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61在这种情况下-
stuff = {'ropes': 1, 'torches': 1, 'gold coin': 42, 'daggers': 1, 'arrow': 0}在哪里-
{'ropes': 1, 'torches': 1, 'daggers': 1}您需要从复数生成正确的单数形式。
因此,对前面的代码进行更多的扩展,我得到-
import inflect
p = inflect.engine()
stuff = {'ropes': 1, 'torches': 1, 'gold coin': 42, 'daggers': 1, 'arrow': 0}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value != 1:
key = p.plural(key)
else:
key = p.singular_noun(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)这将提供以下输出:
Inventory:
1 rope
1 torch
42 gold coins
1 dagger
0 arrows
Total number of items: 45发布于 2022-01-04 04:10:09
我坚持这本书的方法,因为它已经提供了除该块之外的大部分代码,但是我在我的块中包含了默认的和方法,所以我不必创建一个全局变量设置为false-y值。我使用f‘’string,这样就可以轻松地以所需的格式打印键和值,而不是使用pprint模块。
Inventory_List = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inv):
print(f'Inventory:')
for key, value in inv.items():
total = sum(inv.values())
print(f'{value} {key}')
print(f'Total: {total} items')
display_inventory(Inventory_List)https://codereview.stackexchange.com/questions/222354
复制相似问题