6

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
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
BigPy
  • 127
  • 1
  • 2
  • 9
  • 2
    Use `list.items()` instead of `list`. As a side note avoid using `list` as a variable name; it overwrites a builtin type. – Holloway May 30 '19 at 09:08
  • 1
    This has nothing to do with `tkinter`. – Henry Yik May 30 '19 at 09:08
  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – Loïc May 30 '19 at 09:09
  • Another (unrelated) thing to be aware of: you're parsing the user input to an int, but the keys in your dictionary are strings so they'll never match. – Holloway May 30 '19 at 09:10
  • also, the whole idea to use dict is to get the value without the need to iterate over keys. You can use `dict.get()` method to handle for missing key. – buran May 30 '19 at 09:11
  • Hey, Holloway, thank you. Yes, I figured that out right about now. Appreciate your help. – BigPy May 30 '19 at 09:15

3 Answers3

4

You have to use list.items()

for i, j in list.items():
...
Underoos
  • 4,708
  • 8
  • 42
  • 85
Vlad Balan
  • 73
  • 8
3

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).

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Ibtihaj Tahir
  • 636
  • 5
  • 17
2

Please try with items() as you are traversing over dict

for j,i in list.items():
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61