8-8 用户的专辑
1. 项目
在为完成练习 8-7 编写的程序中,编写一个 while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数 make_album(),并将创建的字典打印出来。
在这个 while 循环中,务必要提供退出途径。
2. 代码
def make_album(singer_name, album_name, music_quantity= ''): album = {'singer_name': singer_name, 'album_name': album_name} if music_quantity: album['music_quantity'] = music_quantity return album while True: print("Please input singer's name") print("(enter 'q' at any time to quit)") sg = input("singer_name: ") if sg == 'q': break al = input("album_name: ") if al == 'q': break album0 = make_album(sg, al) print(album0) album1 = make_album(sg, al, music_quantity=20) print(album1)
3. 执行结果
Please input singer's name (enter 'q' at any time to quit) singer_name: kevin album_name: love {'singer_name': 'kevin', 'album_name': 'love'} {'singer_name': 'kevin', 'album_name': 'love', 'music_quantity': 20} Please input singer's name (enter 'q' at any time to quit) singer_name: q Process finished with exit code 0