我正在制作一个文字游戏。我想要一个库存系统,但我似乎无法输出出字典。到目前为止,这就是我所拥有的:
def inventory():
for numberofitems in len(inventory_content.keys()):
inventory_things = list(inventory_content.keys())
inventory_amounts = list(inventory_content.values())
print(inventory_things[numberofitems])
发布于 2018-07-25 09:04:52
我最喜欢的方式:
import json
print(json.dumps(dictionary, indent=4, sort_keys=True))
发布于 2018-07-25 10:28:05
我喜欢pprint
模块包括在Python中。它可以用来打印对象,也可以格式化一个很好的字符串版本。
import pprint
# Prints the nicely formatted dictionary
pprint.pprint(dictionary)
# Sets 'pretty_dict_str' to
pretty_dict_str = pprint.pformat(dictionary)
但是,听起来你正在打印一份清单,用户可能希望将其显示为更类似于以下内容:
def print_inventory(dct):
print("Items held:")
for item, amount in dct.iteritems():
print("{} ({})".format(item, amount))
inventory = {
"shovels": 3,
"sticks": 2,
"dogs": 1,
}
print_inventory(inventory)
其中的指纹:
Items held:
shovels (3)
sticks (2)
dogs (1)
https://stackoverflow.com/questions/-100005710
复制相似问题