Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python自动化枯燥的东西的任务第5章_Python_Dictionary - Fatal编程技术网

使用Python自动化枯燥的东西的任务第5章

使用Python自动化枯燥的东西的任务第5章,python,dictionary,Python,Dictionary,我是一个学习python的新手,我的新书是Albert Sweigart的《用python自动化无聊的东西》 inventory = {'arrows': 12, 'gold coins': 42, 'rope': 1, 'torches': 6, 'dagger': 1} 根据这本词典,我需要这样输出: 目录: 12支箭 42枚金币 1根绳子 6支手电筒 1把匕首 项目总数:62 到目前为止,我制作了类似的东西,并尝试使用书中的方法: inventory = {'arrows': 12,

我是一个学习python的新手,我的新书是Albert Sweigart的《用python自动化无聊的东西》

inventory = {'arrows': 12, 'gold coins': 42, 'rope': 1, 'torches': 6, 'dagger': 1}
根据这本词典,我需要这样输出:

目录:
12支箭
42枚金币
1根绳子
6支手电筒
1把匕首
项目总数:62
到目前为止,我制作了类似的东西,并尝试使用书中的方法:

inventory = {'arrows': 12, 'gold coins': 42, 'rope': 1, 'torches': 6, 'dagger': 1}

def displayInventory(inventory):
    totalNum = 0
    for k, v in inventory.items():
        print(v, k)
        totalNum = totalNum + sum(inventory.values())
    print("Total items of inventory: ")
    return totalNum

print("Your inventory: ")
print(displayInventory(inventory))
输出:

您的库存:
12支箭
42枚金币
1根绳子
6支手电筒
1把匕首
存货项目总数:
310

为什么我的
totalNum
这么大?

字典中有5个元素。您的值为(12+42+1+6+1)=62。62*5=310

每次检查一个项目时,您都要计算所有inventory.values()的总和。如果您是在循环中进行的,那么它应该是

totalNum = totalNum + v
def displayInventory(inventory):
    for k, v in inventory.items():
        print(v, k)
    print("Total items of inventory: ")
    return sum(inventory.values())
如果你想在循环之外做,那么它应该是

totalNum = totalNum + v
def displayInventory(inventory):
    for k, v in inventory.items():
        print(v, k)
    print("Total items of inventory: ")
    return sum(inventory.values())

字典里有5个元素。您的值为(12+42+1+6+1)=62。62*5=310

每次检查一个项目时,您都要计算所有inventory.values()的总和。如果您是在循环中进行的,那么它应该是

totalNum = totalNum + v
def displayInventory(inventory):
    for k, v in inventory.items():
        print(v, k)
    print("Total items of inventory: ")
    return sum(inventory.values())
如果你想在循环之外做,那么它应该是

totalNum = totalNum + v
def displayInventory(inventory):
    for k, v in inventory.items():
        print(v, k)
    print("Total items of inventory: ")
    return sum(inventory.values())

您得到的
totalNum
与预期不同,因为在每次迭代中,您将
totalNum
的值增加
62
,因为
sum(inventory.values())
将返回
62
。因此,在五次迭代之后,
totalNum
将成为
310

如果您想要更紧凑的代码版本,请使用:

print("Inventory:")
print("\n".join(f"{v} {k}"for k, v in inventory.items()))
print("Total number of items:", sum(inventory.values()))
这张照片是:

Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62

您得到的
totalNum
与预期不同,因为在每次迭代中,您将
totalNum
的值增加
62
,因为
sum(inventory.values())
将返回
62
。因此,在五次迭代之后,
totalNum
将成为
310

如果您想要更紧凑的代码版本,请使用:

print("Inventory:")
print("\n".join(f"{v} {k}"for k, v in inventory.items()))
print("Total number of items:", sum(inventory.values()))
这张照片是:

Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62

必须计算for循环后所有项的
总和
,或仅添加循环中当前项的值。必须计算for循环后所有项的
总和
,或仅添加循环中当前项的值。