无效 Python 语法定义函数

Invalid Python Syntax Define Function

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 函数时收到语法错误。我无法确定这段代码有什么问题。

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

一般来说:如果您遇到奇怪的语法错误 - 请查看上一行。