def make_album(name,album_name,song_nums = 1):
dict_album = {name:[album_name]}
if int(song_nums) > 1:
dict_album[name].append(song_nums)
return dict_album
first = make_album('Avril','Fly')
print(str(first)+"\n")
second = make_album('Jay Zhou','Jay Chou\'s bedside story',10)
print(str(second)+"\n")
third = make_album('Tall swift','welcome to New York',12)
print(str(third)+"\n")
while True:
print("\nplease enter the information of album:")
name = input("singer's name:")
album_name = input("album's name:")
song_nums = input("song's number:")
album = make_album(name,album_name,song_nums)
print(album)
上段代码描述了函数的定义和使用:
1.函数定义,用def关键字,注意函数定义的格式
2 函数的返回值可以是列表和字典,总之,是很丰富;这也从侧面反应了:函数的形参也可以是列表和字典
3 函数的形参可以有默认形参,但默认形参通常只能放在()的最后面