vin_bag = {}
option = int(input("PLease enter your option: \n"))
while option != 0:
if option == 1:
artist = input("Enter artist: \n")
song = input("Enter song: \n")
vin_bag[artist] = song
option = int(input("PLease enter your option: \n"))
elif option == 2:
lib_songs = str(len(vin_bag))
print("There are " lib_songs " songs in your music library. \n")
option = int(input("PLease enter your option: \n"))
elif option == 3:
for song in vin_bag:
print(artist, vin_bag[artist])
option = int(input("PLease enter your option: \n"))
選項 3 僅迭代最后一個 key:value 條目而不是整個字典,我可能做錯了什么?
uj5u.com熱心網友回復:
首先,我認為您想將您的選項輸入移到 for 回圈的范圍之外。其次,遍歷字典使用 vin_bag.items()
elif option == 3:
for pair in vin_bag.items():
print(pair)
option = int(input("PLease enter your option: \n"))
uj5u.com熱心網友回復:
elif option == 3:
[print(artist, song) for artist, song in vin_bag]
option = int(input("PLease enter your option: \n"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/412420.html
標籤:
