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 trialmaike willuweit
552 PointsLooping, breaks and continuation and printing
Hi, am stuck with the looping, skipping and continuing in my scrip. I cannot see what i did wrong here...: and I need to print.... Please help in where I did wrong here.. script is attached
def loopy(items):
# Code goes here
for item in items:
if item.index[0] == 'a':
break
continue
else:
print(item)
1 Answer
andren
28,558 PointsThere are two issues, the first is that the task only asks you to skip items using the continue keyword. By using the break keyword as well you stop the loop completely, which is not what the task wants you to do.
The second issue is that you are not supposed to use the index method for this task, the index method is designed such that it will raise an error if it does not find the index of the character you ask it for. Therefore it is not really well suited in situations where you expect the character won't be present most of the time.
In situation like this the better way to check the first character is to use bracket notation directly on the item variable (just like you would on a list) like this: "item[0] == 'a'" that will check if the character at index 0 of the item string equals 'a'.
Fixing those two errors produces this code:
def loopy(items):
# Code goes here
for item in items:
if item[0] == 'a':
continue
else:
print(item)
Which will allow you to complete the challenge.