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

Python Python Collections (Retired) Tuples Combo

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Final question in Tupples

This code executes just fine in Workspaces, but on the console, it seems to be mixing my list and string with a list containing 1-9 and a string containing "Treehouse".

zippy.py
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.

my_list = ['swallow','snake','parrot']
my_string = 'abc'

my_ans = []
def combo(my_list,my_string):
  for index, item in enumerate(my_list):
    tup = (item, my_string[index])
    my_ans.append(tup)
  return my_ans

combo(my_list,my_string)

4 Answers

Hi Elizabeth,

When the code challenge asks you to define a function, it will automatically have its own set of test data, so you don't need to define your own (or put in a call to the function). Because you've defined my_ans outside of the function call, it stores up the information from the previous calls to the function when executing the code.

Just for the sake of experimenting, I'd suggest trying out a couple of things:

1) Call the function multiple times in your code within workspaces; pass in different strings and see what comes out.

2) Move the variable definition to inside the function definition (before the for loop). Try the same thing again.

Your for loop works fine, and there are two ways of modifying the code so it will pass the challenge requirements. Try it out.

vdivekar
vdivekar
3,544 Points

I'm assuming that when you say console, you're talking about the are they give you to type out code underneath the code challenge. If you watch in the videos, the console is the lower box in workspaces, where all lines start with >>>.

The question just wants a function called combo(). Calling the function and passing the iterables into it is not required. Your function works, but there should be the option of passing any two iterables into it. Remove the first two lines of code where you define the iterables, as well as the last one where you call the function. The thing that checks the answers submitted probably uses different strings and lists as inputs.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Oh, sorry about that, what do you call the box in Treehouse where you type in your answer to the code challenge?

You 2 seem to have the same suggestion, so I will try it. But I feel like I have been able to define test data and call functions without a problem on previous challenges.

I keep making the mistake of initializing the test data outside of the function. I will move that.

Yep, defining test data and calling functions is usually not a problem on challenges. The problem here is that you've added additional data to the my_ans list that you didn't actually intend to return (and that the code challenge does not want). It's easily solved by ensuring that the variable you call is returned to an empty state every time you call the function.

In general, it's a good programming practice to strive for the fewest lines (and steps) in your code as possible. Programming is a little like golf that way -- the fewer strokes, the better.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Thanks, I got it to work and understand things better now!