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 trialThomas Katalenas
11,033 PointsMake a function first_4
I am stuck I need a hint!
4 Answers
Kenneth Love
Treehouse Guest TeacherPost what you've done.
Thomas Katalenas
11,033 Pointsdef first_4(*p): i = 0 v = [] g = p.pop(0) if g == len(4): print(g) else: False #if i <5: # for i in len(range(p)): # v.append(p[i]) #else: # print(v)
first_4("hi", "there", "man", "tier", "cart")
Dawid Dudzinski
1,633 PointsNote what you are passing to the function:
first_4("hi", "there", "man", "tier", "cart")
This call has 5 arguments.
<p>
The line below is one argument array with 5 elements in it.
first_4(["hi", "there", "man", "tier", "cart"])
It will return: ['hi', 'there', 'man', 'tier']
If properly implemented. <p/>
String is also iterable so you could pass this to your function:
first_4("hi there man tier cart")
Result: 'hi t'
I hope it helps.
Dawid Dudzinski
1,633 PointsWithout giving a solution I will tell you that this is literally one line function. Slicing is smart enough to account that your object may not be long enough to have 4 elements.
Also, drop the asterisk from function definition.
Respond back if you are unable to proceed.
Thomas Katalenas
11,033 PointsThanks so much I guess I was over thinking it.
What about odds def odds(arg): return arg[::2]
should return odd indexes and it passes in my console, but not the quiz console
Kenneth Love
Treehouse Guest TeacherExcept the problem is to return items with an odd index. [::2]
will return indexes [0, 2, 4, 6, 8, ...]
.