简体   繁体   English

在 function 中使用任意关键字参数时,在打印字典时不会打印所有键值对

[英]while using arbitary keyword argument in a function, all key value pairs are not printed while printing a dictionary

def build_profile(first, last, **info):
profile ={}
profile["first_name"] = first
profile["last_name"] = last
for key, value in info.items():
    profile[key] = value
    return profile

current_profile = build_profile("pawan", "rodriguez", current = "KTM", location = "colombia") print(current_profile) current_profile = build_profile("pawan", "rodriguez", current = "KTM", location = "colombia") print(current_profile)

There are some problems with indentation and the return should be outside of the for loop:缩进有一些问题, return应该在for循环之外:

def build_profile(first, last, **info):
    profile ={}
    profile["first_name"] = first
    profile["last_name"] = last
    for key, value in info.items():
        profile[key] = value
    return profile

current_profile = build_profile("pawan", "rodriguez", current = "KTM", location = "colombia") 
print(current_profile)
def build_profile(first, last, **info):
    profile ={}
    profile["first_name"] = first
    profile["last_name"] = last

    for key, value in info.items():
        profile[key] = value
    return profile

current_profile = build_profile("pawan", "rodriguez", current="KTM", location="colombia") 
print(current_profile)

using return inside for loop only assigns one key word value and exits the function.在 for 循环中使用 return 只会分配一个关键字值并退出 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM