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 trialJames N
17,864 PointsLol! i cheated! but i still need help!
on the first task of this question, my code, as shown below, says that i declared a variable name NumVar and assigned it to (what was originally 0) 6. then i take the items in the list and added them to NumVar, through a for loop, followed by returning NumVar. what i don't understand is why a) the teacher didn't catch the fact that i could simply change my variable to 6 and get through the task, and b) why the for loop doesn't work.
basically, i wanted to inform the teacher of this bug, and also wanted to know how to get through that task properly.
my code is below:
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
NumVar = 6
def add_list(list):
for item in list:
NumVar + item
return NumVar
2 Answers
Josh Keenan
20,315 PointsYou have some bugs in your code! Okay, firstly, "list" is what is known as a reserved word in Python, this means it has a function and can't be used to name anything as doing so calls the function. Change "list" to be "list1" or "my_list" or something of the like.
Secondly, you should declare your variable in the function:
def add_list(my_list):
num_var = 0
Now it will act as a count for your for loop.
Here's what it should look like completed:
def add_list(list1):
total = 0
for item in list1:
total += item
return total
Hope it helped! Post to this thread again if you need help with part two!
Josh Keenan
20,315 PointsLearn one language at a time, syntax isn't the hard part, the hard part is understanding the algorithmic thinking; and no, you don't need to declare the data type!
Josh Keenan
20,315 PointsTagging Kenneth Love to help him spot this bug, but at the end of the day you only are cheating yourself, you are the one who is losing the opportunity to ensure you're learning!
James N
17,864 PointsJames N
17,864 Pointsthanks! i hope the teacher can prevent the bug got me to task 2 by cheating from happening in the first place! i've already gotten through the challenge. task 2 wasn't hard. i thought that in the argument part you had to put in a specific datatype (similar to Java or C#) so that python knows what's going in there. i think i'm trying to cram too many similar languages into my head!