第6章作业
习题6-1
>>> person = {'first_name':'Alice', 'last_name': 'Bob', 'age':18, 'city':'Guangzhou'}
>>> person
{'first_name': 'Alice', 'last_name': 'Bob', 'age': 18, 'city': 'Guangzhou'}
>>> print(person['first_name'])
Alice
>>> print(person['last_name'])
Bob
>>> print(person['age'])
18
>>> print(person['city'])
Guangzhou
习题6-6
favorite_languages = {
'jen': 'python',
'serah': 'c',
'edward': 'ruby',
'phil': 'python',
}
people = ['alice', 'jen', 'bob', 'phil']
for person in people:
if person not in favorite_languages.keys():
print(person + ', please take our poll!')
else:
print(person + ', thank you for taking the poll.')
Output:
alice, please take our poll!
jen, thank you for taking the poll.
bob, please take our poll!
phil, thank you for taking the poll.
习题6-8
favorite_places = {
'alice': ['Guangzhou', 'Shenzhen', 'Beijing'],
'bob': ['Hangzhou', 'Shanghai'],
'ben': ['Chengdu'],
}
for person, places in favorite_places.items():
print('Person: ' + person)
print('Places:')
for i in places:
print(i)
print('')
Output
Person: alice
Places:
Guangzhou
Shenzhen
Beijing
Person: bob
Places:
Hangzhou
Shanghai
Person: ben
Places:
Chengdu