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