python第六章习题

6-3

cihuibiao = {'for' : 'xunhuan','if' : 'judge','while' : 'xunhuan','int' : 'zhengshu','char' : 'letter'}
for key,value in cihuibiao.items():
	print(key + " : " + value)

输出结果:

int : zhengshu
while : xunhuan
char : letter
for : xunhuan
if : judge

6-5

rivers = {'Nile' : 'Egypt','Changjiang' : 'China','Amazon' : 'Brazil'}
for key,value in rivers.items():
	print("The " + key + " runs through " + value)

for key in rivers.keys():
	print(key)

for value in rivers.values():
	print(value)

输出结果:

The Amazon runs through Brazil
The Changjiang runs through China
The Nile runs through Egypt
Amazon
Changjiang
Nile
Brazil
China
Egypt

6-8

Tom = {
	'name' : 'Tom',
	'type' : 'cat',
	'owner' : 'Kangkang'
}

Jake = {
	'name' : 'Jake',
	'type' : 'mouse',
	'owner' : 'Bob'
}

Wzx = {
	'name' : 'Wzx',
	'type' : 'monkey',
	'owner' : 'Yzh'
}

pets = [Tom,Jake,Wzx]

for pet in pets:
	for key,value in pet.items():
		print(key + " : " + value)

输出结果:

owner : Kangkang
type : cat
name : Tom
owner : Bob
type : mouse
name : Jake
owner : Yzh
type : monkey
name : Wzx

6-9

Nanking = {
	'name' : 'Nanking',
	'country' : 'China',
	'population' : "653w",
	'fact' : 'Nanking Massacre'
}

London = {
	'name' : 'London',
	'country' : 'England',
	'population' : '50w',
	'fact' : '2012 Olypimc'
}

Sichuan = {
	'name' : 'Sichuan',
	'country' : 'China',
	'population' : '8204w',
	'fact' : '2008 earthquake'
}

cities = [Nanking,London,Sichuan]

for city in cities:
	for key,value in city.items():
		if key == 'name':
			print("The city's name is " + value)
		elif key == 'country':
			print(city['name'] + " locates in " + value)
		elif key == 'population':
			print("The population of " + city['name'] + " is " + value)
		else:
			print(value + " happened in " + city['name'])

输出结果:

Nanking Massacre happened in Nanking
Nanking locates in China
The city's name is Nanking
The population of Nanking is 653w
2012 Olypimc happened in London
London locates in England
The city's name is London
The population of London is 50w
2008 earthquake happened in Sichuan
Sichuan locates in China
The city's name is Sichuan
The population of Sichuan is 8204w


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值