Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Chang
5,214 PointsPython Tuple: ValueError too many values to unpack (expected 4)
I tried to use a tuple to record the statistics of the twitch top streamer, but it appears the value error. Despite the fact that I checked for several times, I couldn't discover the problem. I would be grateful to anyone who answered my question. Additionally, how people pronounce the word "Tuple"? Thanks.
my code:
streamers = input("Enter a streamer's name")
tp = ("shroud", "chocotaco", "just9n", "Lurn", "wadu", "DrDisrespect")
tp [2:6:1]
shroud = ("shroud", "10,037", "6,473,519", "63,119")
chocotaco = ("chocotaco", "4,863", "817,166", "12,924")
just9n = ("just9n", "Unknown", "331,000", "9,887")
name, paid_subscribers, followers, peak_viewers = streamers
name, paid_subscribers, followers, peak_viewers
result:
Enter a streamer's name: shroud
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
10 chocotaco = ("chocotaco", "4,863", "817,166", "12,924")
11 just9n = ("just9n", "Unknown", "331,000", "9,887")
---> 12 name, paid_subscribers, followers, peak_viewers = streamers
13 name, paid_subscribers, followers, peak_viewers
ValueError: too many values to unpack (expected 4)
1 Answer
Jeff Muday
Treehouse Moderator 28,724 PointsThe error message "too many values to unpack" happens when you tried to pull out more elements from the tuple than existed.
# note The input() function only returns ONE string.
streamers = input("Enter a streamer's name")
# so this will throw an error, because streamers ONLY has one string, not the 4 strings you attempt to assign
name, paid_subscribers, followers, peak_viewers = streamers
Here is a program that loops through a list of streamers asks the user to enter a name and it searches the list and unpacks into the four variables
# here is a list of tuples.
# each tuple stands for a particular streamer
streamers = [
("shroud", "10,037", "6,473,519", "63,119"),
("chocotaco", "4,863", "817,166", "12,924"),
("just9n", "Unknown", "331,000", "9,887")
]
while True:
found = False
user_input = input("Enter a streamer's name (or quit): ")
if user_input == 'quit':
break
for streamer in streamers:
# we can unpack the four values from the tuple here.
name, paid_subscribers, followers, peak_viewers = streamer
# check if the name matched the user_input
if name == user_input:
# ok, the name matched, print it out.
found == True
# show the streamer
print("Name: ", name)
print("Paid Subscribers: ", paid_subscribers)
print("Followers: ", followers)
print("Peak viewers: ", peak_viewers)
if not found:
print("name not found.")
So, if you wanted to put in several values look at this example
streamers = []
while True:
print("Enter the data for a streamer, separate each value with a space or type quit")
print("name paid_subscribers followers peak_viewers")
user_input = input("> ")
if user_input == "quit":
break
user_tuple = tuple(user_input.split())
streamers.append(user_tuple)
for streamer in streamers:
print(streamer)