问题是,已经有了一个字典叫travel_log,然后我想加新的key进去,但是为什么要在空字典new_country前面加一个def呢,为什么不直接写一个空的字典然后直接赋值,我知道我的不对,但是我不明白为什么要加def
我的
travel_log = [
{
"country": "France",
"visits": 12,
"cities": ["Paris", "Lille", "Dijon"]
},
{
"country": "Germany",
"visits": 5,
"cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
#TODO: Write the function that will allow new countries
#to be added to the travel_log. 👇
new_country = {}
new_country["country"] = Russia
new_country["visits"] = 12
new_country["cities"] = Moscow, Saint Petersburg
travel_log.append(new_country)
print(travel_log)
正确的
travel_log = [
{
"country": "France",
"visits": 12,
"cities": ["Paris", "Lille", "Dijon"]
},
{
"country": "Germany",
"visits": 5,
"cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]
#TODO: Write the function that will allow new countries
#to be added to the travel_log. 👇
def add_new_country(country_visits, times, cities_visits):
new_country = {}
new_country["country"] = country_visits
new_country["visits"] = times
new_country["cities"] = cities_visits
travel_log.append(new_country)
add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)