Python入门习题大全——用户的专辑

Python入门习题大全——索引

在为完成练习“专辑”编写的程序中,编写一个while循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album(), 并将创建的字典打印出来。在这个while循环中,务必要提供退出途径。

# 用户的专辑
def make_album(name, age, num):
    people = {'name': name, 'age': age, 'num': num}
    return people

while True:
    print("Please enter message: ")
    print("(enter 'q' at any time to quit)")
    name = input('name: ')
    if name == 'q':
        break
    age = input('age: ')
    if age == 'q':
        break
    num = input('num:')
    if num == 'q':
        break
    message = make_album(name, age, num)
    print(message)

输出为:
(依次输入ling,19,7,q)

Please enter message: 
(enter 'q' at any time to quit)
name: ling
age: 19
num:7
{'name': 'ling', 'age': '19', 'num': '7'}
Please enter message: 
(enter 'q' at any time to quit)
name: q

Process finished with exit code 0
发布了269 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43479432/article/details/105425536