第八章练习题

8-3

def make_shirt(size, shape):
    """show the size and shape of the shirt"""
    print("The size of the shirt is " + str(size) + " and the shape of the shirt is " + shape +".")
   
 

make_shirt(shape = 'square',size = '5')
make_shirt(4,'square')

The size of the shirt is 5 and the shape of the shirt is square.

The size of the shirt is 4 and the shape of the shirt is square.

------------------

(program exited with code: 0)

Press return to continue

8-8

def make_album(singer, album, number):
	"""show the information of the album"""
	if number:
		album_information = {'singer': singer, 'album': album, 'number': number}
	else:
		album_information = {'singer': singer, 'album': album}
	return album_information


while True:
	print("If you want to quit, please enter 'q'.")
	singer = raw_input("Please enter the name of singer: ")
	album = raw_input("Please enter the name of album: ")
	number = raw_input("Please enter the number of songs: ")
	if singer == 'q':
		break;
	else:
		information = make_album(singer,album,number)
		print(information)
If you want to quit, please enter 'q'.
Please enter the name of singer: a
Please enter the name of album: v
Please enter the number of songs: 3
{'album': 'v', 'singer': 'a', 'number': '3'}
If you want to quit, please enter 'q'.
Please enter the name of singer: 3
Please enter the name of album: d
Please enter the number of songs: 
{'album': 'd', 'singer': '3'}
If you want to quit, please enter 'q'.
Please enter the name of singer: q
Please enter the name of album: d
Please enter the number of songs: 3


------------------
(program exited with code: 0)
Press return to continue

8-11

def show_magicians(magicians):
	for magician in magicians:
		print(magician)
		
def make_great(magicians,final_magicians):
	while magicians:
		magician = 'the Great ' + magicians.pop()
		final_magicians.append(magician)
	show_magicians(magicians)
	print("\n")
	show_magicians(final_magicians)
	

magicians = ['a','b','c']
final_magicians = []
show_magicians(magicians)
make_great(magicians[:],final_magicians)
show_magicians(magicians)
make_great(magicians,final_magicians)
show_magicians(magicians)
make_great(magicians[:],final_magicians)
a
b
c


the Great c
the Great b
the Great a
a
b
c


the Great c
the Great b
the Great a
the Great c
the Great b
the Great a


the Great c
the Great b
the Great a
the Great c
the Great b
the Great a


------------------
(program exited with code: 0)
Press return to continue

8-14

def car_information(**item):
	car = {}
	for key,value in item.items():
		car[key] = value
        
        return car

cars = car_information(color = 'red',name = 'bwm')
print(cars)
{'color': 'red', 'name': 'bwm'}


------------------
(program exited with code: 0)
遇到一个问题最开始,

IndentationError: unindent does not match any outer indentation level

找了好久也没有发现语法错误,后来发现是因为缩进的问题,下次一定要注意。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值