1. 程式人生 > >Python程式設計:從入門到實踐(課後習題6)

Python程式設計:從入門到實踐(課後習題6)

# 6-1 人
name = {
	'first_name': 'lili', 
	'last_name': 'zhang', 
	'age': 18, 
	'city': 'guangzhou'
}
print(name['first_name'] + ', ' + name['last_name'] + ', ' + 
	str(name['age']) + ', ' + name['city'])

# 6-2 喜歡的數字
nums = {'kobe': 24, 'eason': 7, 'jordan': 23, 'johnson': 32, 'o\'neal': 34}
for i, j in nums.items():
	print(i.title() + '\'s favorite number is ' + str(j) + '.')  # 注意items後的括號

# 6-3 詞彙表
dicts = {
	'print': '輸出', 
	'if': '如果', 
	'else': '否則', 
	'True': '真', 
	'False': '假'
}
for key, value in dicts.items():
	print(key + ': ' + value)

# 6-5 河流
rivers = {'nile': 'egypt', 'changjiang': 'china', 'huanghe': 'china'}
for river, nation in rivers.items():
	print('The ' + river.title() + ' runs through ' + nation.title() + '.')
for river in rivers.keys():
	print(river)
for nation in rivers.values():
	print(nation)

# 6-6 調查
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}
invatied = ['sarah', 'phil', 'jason', 'decula']  # 6-6-1
for i in invatied:
	if i in favorite_languages:
		print(i.title() + ",thank you for accepting the invitation.")
	else:
		print(i.title() + ",please accept your investigation.")


# 6-7 人
name1 = {'first_name': 'lili', 'last_name': 'zhang', 'age': 18, 'city': 'guangzhou'}
name2 = {'first_name': 'kai', 'last_name': 'zhou', 'age': 20, 'city': 'guangzhou'}
name3 = {'first_name': 'jie', 'last_name': 'liu', 'age': 20, 'city': 'guangzhou'}
people = [name1, name2, name3]
for name in people:
	for i, j in name.items():
		print(i + ': ' + str(j))
	print('\n')

# 6-8 寵物
dog = {'dog': 'zhangsan'}
cat = {'cat': 'lisi'}
pig = {'pig': 'wangwu'}
pets = [dog, cat, pig]
for pet in pets:
	for x, y in pet.items():
		print(x + ': ' + y)

# 6-9 喜歡的地方
favorite_places = {
	'zhanglili': ['lijiang', 'fenghuang', 'qingdao'], 
	'zhoukai': ['qinghai', 'mohe'], 
	'liujie': ['lasa', 'hulunbeier', 'yangshuo']
}
for name, places in favorite_places.items():
	print(name.title() + " favorite place is: ")
	for place in places:
		print(place.title())
	print('\n')

# 6-10 喜歡的數字
favorite_nums = {
	'kobe': [8, 10, 24] , 
	'eason': [7, 18], 
	'jordan': [0, 23], 
	'johnson': [10, 32], 
	'o\'neal': [32, 34]
}
for name, nums in favorite_nums.items():
	print(name.title() + " favorite numbers is: ")
	for num in nums:
		print(str(num))
	print('\n')

# 6-11 城市
cities = {
	'Chenzhou': {'country': 'China', 'population': 13.75, 'fact': '''The 
	People's Republic of China is located in the eastern part of Asia, the 
	Pacific West Bank, is the working class led by the workers and peasants 
	Union-based people's democratic dictatorship of the socialist countries.'''}, 
	'Los angels': {'country': 'America', 'population': 3.231, 'fact': '''The 
	United States is a highly developed capitalist country, in the two world 
	wars, the United States and other allies to win, after decades of cold war,
	 after the disintegration of the Soviet Union, became the only superpower,
	  in the economy, culture, industry The field is in the world's leading 
	  position.'''}, 
	  'Amstel dam': {'country': 'Holland', 'population': 0.1702, 'fact': '''The 
	  Netherlands is a highly developed capitalist country known for its 
	  seawalls, windmills, tulips and tolerant social atmosphere, and the 
	  world is the most liberal in the laws governing drugs, sexual 
	  intercourse and abortion.'''}
}
for city, informations in cities.items():
	print('city: ' + city)
	for key, value in informations.items():
		print(key + ': ' + str(value))
	print('\n')