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')


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值