【问题标题】:SyntaxError: keyword can't be an expression in function call python3SyntaxError:关键字不能是函数调用python3中的表达式
【发布时间】:2026-02-13 00:00:02
【问题描述】:
def orders(**sandwich):
    sand={}
    for category,toppings in sandwich.items():
        sand[category]=toppings
        return sand 

    orders('cheese'='extra','toppings'='mushroom')
    print(orders)       

这是我为一个小型项目编写的代码,但我得到了这个错误 语法错误:关键字不能是表达式 请纠正它,我是新手,所以请解释一下 谢谢!

【问题讨论】:

    标签: python-3.x function dictionary


    【解决方案1】:

    关键字参数必须是标识符,而不是字符串:

    orders(cheese='extra', toppings='mushroom')
    

    另外,请注意,您将在循环的第一次迭代中返回 sand

    而且,sandwich 已经是一个字典。实际上你的函数可以替换为:

    d = dict(cheese='extra', toppings='mushroom')
    print(d)
    

    【讨论】:

      最近更新 更多