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 trialHassan Baukman
2,288 PointsWhy do I keep getting an error message on my code?
I checked my code in workspaces and it runs fine!
def loopy(items):
# create the new list
items=[]
print("What do you want to add to the list? ")
#Get the new items
new_items=input("> ")
#place new items into the items list
items.append(new_items)
#display the list
for new_items in items:
print(items)
loopy("items")
3 Answers
Rich Zimmerman
24,063 PointsIt's probably because you're defining an empty list inside the function called "items" but also passing a parameter called "items" so when you call "items" later, like in your for loop, it's conflicting and throwing an error.
Steve Hunter
57,712 PointsHi Hassan,
The challenge asks you to write a method that uses a for
loop to print out each item within the items
parameter passed in. That's not what your code does so your code will fail the tests behind the challenge.
For the first task you have the beginnings of a method called loopy
that receives items
as a parameter. You need to create the for
loop to print every item.
You've created a for
loop but not quite correctly as you've used a variable that already holds something. You've also erased the contents of the incoming list by declaring items = []
. The code challenge just needs a loop:
def loopy(items):
# Code goes here
for item in items:
print(item)
The for
loop iterates over each element inside items
at each pass it stores each individual element in the item
variable (you could call it anything, like i
or whatever). So, just print
the value that item
holds. The second task adds a little more to this, but the above will get you there.
I hope that helps,
Steve.
Hassan Baukman
2,288 PointsThank you for your support I see the problem now!
Hassan Baukman
2,288 PointsHassan Baukman
2,288 PointsThank you for your response. I see the issue now.