将用户输入存储为函数参数

-1 投票
0 回答
52 浏览
提问于 2025-04-12 08:53

这个能用吗?我正在尝试写一个循环,让用户输入专辑的艺术家和标题。一旦我有了这些信息,我就可以用用户输入的内容调用一个叫做make_album的函数,并打印出生成的字典。我还需要在这个循环中加入一个退出的选项。

这是我的代码:

def make_album(name, title, number = None):
    """Return a dictiomary containing artist name and album title"""
    dictionary = {}
    dictionary["Artist"] = artist_name
    dictionary["Title"] = album_title
    dictionary["Number of songs"] = song_number   
    print_dict = print(dictionary)
    return print_dict 


while True:
    artist_name = input("\nPress 'q' to quit. \nWhat is the name of your favorite artist? ")
    if artist_name == 'q':
        break

    album_title = input("\nPress 'q' to quit. \nWhat is the title of your favorite album? ")
    if album_title == 'q':
        break

    song_number = input("\nPress 'q' to quit. \nHow many songs are in your favorite album? ")
    if song_number == 'q':
        break
   
    make_album(artist_name, album_title, song_number)

    another_entry = input("Would you like to input another entry? (yes/no) ")
    if another_entry == "no":
        break

0 个回答

暂无回答

撰写回答