无效的Python语法定义函数

时间:2016-02-20 22:16:40

标签: python python-3.x syntax syntax-error

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函数时收到语法错误。我无法确定此代码的错误。

1 个答案:

答案 0 :(得分:2)

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

一般情况下:如果你得到一个奇怪的语法错误 - 请看上一行。