Python语法Define函数无效

2024-06-16 11:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

Satchel = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1}
def displayInventory(inventory, leftWidth, rightWidth):
    print('INVENTORY'.center(leftWidth + rightWidth, '-'))
    for k, v in inventory.items():
        print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth))
        item_total = int(item_total) + int(v)
        print('Total number of items: ' + str(item_total)

displayInventory(Satchel, 15, 6)

调用displayInventory函数时收到语法错误。我很难确定这个代码有什么问题。你知道吗


Tags: itemstorchiteminttotalinventorycoinprint
1条回答
网友
1楼 · 发布于 2024-06-16 11:06:15

函数中对print的最后一次调用缺少右括号。此外,在您显示的代码中,在定义名称之前使用的是名称item_total。你知道吗

一般来说:如果你得到一个奇怪的语法错误-看看上一行。你知道吗

相关问题 更多 >