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 trialNancy Melucci
Courses Plus Student 36,143 PointsWorks in IDE, not in challenge
This works fine in PyCharm. Fails in Code Challenge, again with the very unhelpful "Bummer. Try Again"
1) What am I missing? 2) Could something be done to make the feedback more informative. I normally love the challenges but when I get this feedback message 20 times, I feel like throwing my computer. Can't the tester say something more useful?
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(myiter1, myiter2):
combo_list = []
for index, item in enumerate(myiter2):
combo_list.append((myiter1[index], item))
return combo_list
6 Answers
Kenneth Love
Treehouse Guest TeacherHmm, so...this is fun :)
First off, Nancy Melucci, your solution is almost exactly what I wrote in my tests (which don't require the code to look any particular way, btw). But, by using enumerate()
on the second iterable instead of the first, you discovered a weird "bug" in my testing.
One of the tests that I send to your combo()
function is a dict and a list. So your combo()
tries to get an item with a key of 0 from the dict...which doesn't exist. So, time to update the test to prevent that scenario!
Jason Anello
Courses Plus Student 94,610 PointsHi Kenneth,
I didn't read the instructions carefully enough to realize it has to work with dicts, sets, etc... that you can't access by index.
Now I'm wondering how we could code this for dicts based on knowledge gained up to this point.
Nancy's solution wouldn't work if the first argument is a dictionary and my suggested enumerate solution won't work if the second argument is a dictionary. The range() solution wouldn't work for similar reasons.
Are you restricting the tests now to tuples, lists, and strings or are dicts still valid?
Would it be valid to convert the arguments to lists using list()
and then use the enumerate or range solution?
Umesh Ravji gave an answer here https://teamtreehouse.com/community/combo-4
suggesting that the arguments be converted to iterators. But then don't we have to use generators or maybe repeatedly call next()
until a StopIteration error is raised?
Kenneth Love
Treehouse Guest TeacherHey Jason Anello,
I don't want to give too much away about how the grader is working, but, yes, it's currently missing any non-ordered iterables.
I don't think it's fair to rely on people using iter()
and the like because those haven't been covered. While I'm fine with making y'all look up documentation, most people wouldn't even know where to start for iter()
(or zip()
for that matter).
I'm going to leave the challenge as-is for now and maybe we'll revisit using it for non-ordered iterables later.
Umesh Ravji
42,386 PointsHi, Kenneth Love and Jason Anello
I did mention I thought it might not be the best way to do this, but I couldn't see any way (a way that made more 'sense') of achieving the answer without it and without taking advantage of zip :)
def combo(a, b):
iter_a = iter(a)
iter_b = iter(b)
results = []
while True:
try:
results.append((iter_a.next(), iter_b.next()))
except StopIteration:
break
return results
I think I used something to that effect the other day, scary looking now though :p
Jason Anello
Courses Plus Student 94,610 PointsHi Nancy,
You were on the right track with your solution and I think it produces the correct result. In my opinion, that code should be passing. This challenge seems to be a little too rigid on how the solution is coded.
If you change it to enumerate over iter1 instead and update the append argument accordingly, it will pass.
def combo(myiter1, myiter2):
combo_list = []
for index, item in enumerate(myiter1):
combo_list.append((item, myiter2[index]))
return combo_list
This is essentially the same as your solution but this one will pass.
The original way I tried to code this was -
def combo(myiter1, myiter2):
combo_list = []
for index in range(len(myiter1)):
combo_list.append((myiter1[index], myiter2[index]))
return combo_list
This one uses range to generate the index values and it's only slightly different than your solution but it does not pass either.
Philip Gales
15,193 PointsI think this is what you were aiming for with your code. Zip works great, but I am not sure if they have taught that yet.
def combo(myiter1, myiter2):
combo_list = []
counter = 0
for item1 in myiter1:
item2 = myiter2[counter]
output = (item1, item2)
combo_list.append(output)
counter += 1
return combo_list
Jason Anello
Courses Plus Student 94,610 PointsHi Philip,
range() and enumerate() have both been introduced at this point which can make the code shorter by eliminating the need for a manual counter variable
Alexander Davison
65,469 PointsFirst, (thankfully) this challenge requires only a mere two lines of code.
Second, your code working in your IDE (or Python Shell) it doesn't mean it will pass the challenge.
There's a built-in zip
function that does exactly what the challenge is asking for:
def combo(a, b):
return list(zip(a, b))
(You should be swatting your forehead or laughing out loud when you see this code pass)
I hope this (sort of) helps :P
~Alex
Jason Anello
Courses Plus Student 94,610 PointsHi Alexander,
I think it's fine to mention the zip() function as an alternative solution but it wasn't mentioned in the course as far as I can recall.
I think the point of the challenges is to see if you can apply the concepts that were taught in the videos.
Kenneth Love
Treehouse Guest TeacherAdded a test to make sure you don't use zip()
:) Yes, it exists and I like to avoid re-inventing wheels, but the point is to solve problem on your own.
Nancy Melucci
Courses Plus Student 36,143 PointsRight...he (Love) didn't show us zip. He used enumerate. I would rather use zip but...there you go. I still wish that the compiler would say more. Python is supposed to be "easy" but it hides more of its functionality than other languages. I am not brilliant at any of this, but I have to struggle more with Python, despite the fact I've been told it's the great "beginner's language."
Thanks.
Philip Gales
15,193 PointsIn many challenges, they do not use all of the code they previously used. They are more of an 'are you getting it before we move on' type of quiz. You are correct in assuming the previous video is generally a good place to start.
Nancy Melucci
Courses Plus Student 36,143 PointsThank you kindly Prof. Love. I want to finish this course. I want to be able to code in Python. Someday!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsTagging Kenneth Love
Is this challenge requiring the solution to be coded a certain way?