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 trialRakshith Nayak
1,893 PointsMake a function named first_4 that accepts an iterable as an argument and returns the first 4 items in the iterable.
Why can't I put in numbers in my argument. I got the answer right if I put in a word in my argument but with numbers I don't. Any input would be appreciated. thank you
def first_4(123456):
return 123456[:4]
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! It's because the thing in parentheses is what we call a parameter and it must be a valid python variable name. 123456 is not a valid Python variable name. You can think of the parameter as a variable declaration that lasts for the execution of the function/method. When we call the function/method we will send in a piece of data. This data will now be referenced by the name we gave it as the parameter. Hope this helps!
Rakshith Nayak
1,893 PointsThat was very helpful. Thank you very much
Rakshith Nayak
1,893 PointsRakshith Nayak
1,893 PointsThank you for your response but I have a question here. So must it(parameter) always be referenced with a name and not a number?
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherYes, Rakshith. And it's because the parameter must be a valid Python variable name. 23 is not a valid variable name. Take a look at this method:
If you run this code it will print 150 to the screen. We send in 100 and 50.
x
is now equal to 100 inside that function andy
is now equal to 50. Remember long ago when you did things likex = 100
andy = 50
? This is similar to that except that it's receiving the values from elsewhere and setting the parameters equal to those values. As such this means that those parameter names must be something that you could normally use for a variable name.If you try and run this:
You will receive a compiler error. This is because neither 100 nor 50 are valid variable names-
Hope this clarifies things!