Python 2.7 为什么我的代码没有打印字典中给出的所有值?
我写了一个函数,它提供了汽车的所有细节。该函数接受制造商、型号和任意数量的参数,并存储在字典中。当我在函数调用中提供任意值时,它只打印提供的三个值中的一个值 这是针对linux mint 19.1肉桂geany Ide的Python 2.7 为什么我的代码没有打印字典中给出的所有值?,python-2.7,Python 2.7,我写了一个函数,它提供了汽车的所有细节。该函数接受制造商、型号和任意数量的参数,并存储在字典中。当我在函数调用中提供任意值时,它只打印提供的三个值中的一个值 这是针对linux mint 19.1肉桂geany Ide的 def car(manufacturer,Model, **features): """Details of the car""" car_profile={} car_profile['manufacturer']= manufacturer
def car(manufacturer,Model, **features):
"""Details of the car"""
car_profile={}
car_profile['manufacturer']= manufacturer
car_profile['Model']= Model
for key, value in features.items():
car_profile[key]=value
return car_profile
car_info=car('honda', 'accord', year=1991, color='white',
headlights='popup')
print(car_info)
结果应显示字典中的所有键值对。您将在for循环中返回您的汽车配置文件。你想做的是
def car(manufacturer,Model, **features):
"""Details of the car"""
car_profile={}
car_profile['manufacturer']= manufacturer
car_profile['Model']= Model
for key, value in features.items():
car_profile[key]=value
return car_profile