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 trialDuarte Reis
Python Web Development Techdegree Student 897 PointsHow to print the current member?
I think I'm printing all the list and not the current member. My code so far is this:
def loopy(items): list1 = [items]
for items in list1: if items.index[0]=="a": continue print(items)
How do I print just the current member? Or is there any other problem with the code?
Thanks,
Duarte
def loopy(items):
list1 = [items]
for items in list1:
if items.index[0]=="a":
continue
print(items)
2 Answers
Duarte Reis
Python Web Development Techdegree Student 897 PointsHi Ryan! Thanks a lot for the brilliant explanation.
It was perfect and it worked.
;)
Ryan S
27,276 PointsHi Duarte,
You are on the right track but, there are a few issues in your code that need to be cleared up.
The first thing is that in the function definition, the argument "items" is already a list. So you don't need to try to convert it to a list as you are doing. So you can get rid of the "list1" variable entirely and just work with "items".
The second thing is that your for loop is not contained inside your function. Indentation is crucial in Python and if you want a statement to be nested inside of another one, it needs to be indented. Right now your function ends at the "list1" statement and the for loop is an entirely separate block of code.
Also, regarding your for
loop, the question is specific in its wording and gives a hint on how to set it up: "Loop through each item in items..."
Another thing is that you are using "index" incorrectly. The .index() method will return the index number of whatever you are looking for.
Example:
my_list = ['a', 'b', 'c']
my_list.index('a') # would return 0
my_list.index('b') # would return 1
So if you want to do the opposite and get the item located at a certain index, you'd use square brackets, similarly to how you are using them, but without the .index().
Example:
my_list = ['a', 'b', 'c']
my_list[0] # would return 'a'
my_list[1] # would return 'b'
And the last thing is that you will need to make use of an "else" statement. Printing the "item" in "items" should only happen if your if
statement isn't true. So print(item)
should not be indented under your if
statement, but should be under a separate else:
statement.
Hope this clears things up. Good luck.
Ryan S
27,276 PointsRyan S
27,276 PointsHey no problem. Glad it worked out for you!