简体   繁体   中英

How to fix "ValueError: not enough values to unpack (expected 2, got 1)"

I am trying to make a music app using tkinter and python, but I am not able to get rid of "ValueError: not enough values to unpack (expected 2, got 1)" bug. Have a look at my code and you'd be much clear with what I am dealing.

The mechanism is pretty simple, I, at first, display the song options via dictionary(list), and after taking the input, corresponding value of "j",(example, if input is 1 then j is one and corresponding value of j is i) to be saved as the name of the song and execute the program by playing the music.

list = {
    '1':'Say You Won t Let Go.mp3','2':'In the Jungle the mighty jungle.mp3'
}
lost = ''
print(list)
print("which one?")
this_one = int(input(''))
for j,i in list:
    if j == this_one:
        lost = i

You have to use list.items()

for i, j in list.items():
...

当您遍历dict请尝试使用items()

for j,i in list.items():

Here you go,

songs = {"1": "Say You Won t Let Go.mp3",
         "2": "In the Jungle the mighty jungle.mp3"}
lost = ''
print(songs)
this_one = int(input("Which One? "))

for number, song in songs.items():
    if number == this_one:
        lost = song

dict.items() returns a tuple of 2 objects, (Keys, values).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM