6-6 调查
favorite_language = {
'jen' : 'Python',
'sarah' : 'c',
'edward' : 'ruby',
'phil' : 'python',
'bill' : 'cpp'
}
people_to_be_polled = ('edward', 'phil', 'steve', 'harry')
for person in people_to_be_polled:
if person in favorite_language.keys():
print(person.title() + ", thank you for your participation.")
else:
print(person.title() + ", I invite you to join the poll.")
6-7 人
person1 = {
'first name' : 'first1',
'last name' : 'last1',
'age' : '20',
'city' : 'city1'
}
person2 = {
'first name' : 'first2',
'last name' : 'last2',
'age' : '40',
'city' : 'city2'
}
person3 = {
'first name' : 'first3',
'last name' : 'last3',
'age' : '60',
'city' : 'city3'
}
people = [person1, person2, person3]
for person in people:
for key, value in person.items():
print(key.title() + ": " + value.title())
print()
6-8 宠物
pet1 = {
'kind' : 'dog',
'owner' : 'dog man'
}
pet2 = {
'kind' : 'cat',
'owner' : 'cat man'
}
pet3 = {
'kind' : 'pig',
'owner' : 'pig man'
}
pet4 = {
'kind' : 'turtle',
'owner' : 'turtle man'
}
pets = [pet1, pet2, pet3, pet4]
for pet in pets:
for key, value in pet.items():
print(key.title() + ": " + value.title())
print()
6-11 城市
cities = {
'beijing' : {
'country' : 'China',
'population' : '21 million',
'fact' : 'capital of China'
},
'guangzhou' : {
'country' : 'China',
'population' : '14 million',
'fact' : 'capital of Guangdong'
},
'shanghai' : {
'country' : 'China',
'population' : '24 million',
'fact' : 'the most prosperous city in China'
}
}
for city, info in cities.items():
print(city.title())
for key, value in info.items():
print(key + ": ", value)
print()