def build_profile(first,last,**user_info):
profile={}
profile['firstname']=first
profile['lastname']=last
for k,v in user_info.items():
profile[k]=v
return profile
user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc')
profile={}
profile['firstname']=first
profile['lastname']=last
for k,v in user_info.items():
profile[k]=v
return profile
user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc')
print(user_profile)
报错:
File "<ipython-input-7-78b8969575f2>", line 9 user_profile=build_profile('yuanqing','li','place'='chengdu',school='uestc') ^
SyntaxError: keyword can't be an expression
将键值对传进函数作为实参传进函数实,不能写'place'='chengdu',应该写place='chengdu',具体理由现在还不清楚,后面来补充。
补充:在操作字典时,键确实需要加‘’,但是此处应该遵守关键字实参的传递规则,关键字不能是expression。