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 trial

Lior Dolinski
3,905 PointsValueError : not enough values to unpack. why?
i tried building a simple function which solves a simple equation. (such as x+4=9)
def solve_eq(equation):
x, add, num1, equal, num2 = equation.split()
num1, num2 = int(num1), int(num2)
return "x = " + str(num2 - num1)
print(solve_eq("x+4=9"))
but all i get is: ValueError: not enough values to unpack (expected 5, got 1)
i know its cause of the split.() but it should work, no? you can assign multiple variables to that split (assuming you know that exactly 5 are going to come out of split)
1 Answer

Benjamin Lange
16,178 PointsBy default, the split() function will split on white space. Since there are no spaces separating each part of the equation, that function call will not work. As far as I know, there isn't a way to split each individual character if there is no character separating the values using the split() function.
Instead, convert the equation into a list:
x, add, num1, equal, num2 = list(equation)
That will assign each variable one of the characters from the equation string that is passed in. Note that this will not work with numbers that contain more than one digits.
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsOr, just:
x, add, num1, equal, num2 = equation
Benjamin Lange
16,178 PointsBenjamin Lange
16,178 PointsOh, awesome! I didn't know you could do that.
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsYeah, neither did I, but I suspected it would work, because Python is awesome! :)
Benjamin Lange
16,178 PointsBenjamin Lange
16,178 PointsIt really is. I came from a C# world and Python makes a lot of things so much easier to do. :)