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 trialANDY VAN DEN BOS
2,330 PointsHow do we use 'skip'?
I am not sure how to even begin this challenge. Did we cover 'skip'?
def loopy(items):
# Code goes here
1 Answer
Travis Bailey
13,675 PointsTook me forever to understand this concept since you're constantly learning the program needs to be explicitly told what to do.
So, to make something skip, you just tell it to do nothing with it when it finds it. Here's an example. Let's say I want to make a function that takes a list and returns the number of cats.
animals = ['cat', 'cat', 'dog', 'cat', 'cat']
def cat_counter(my_list):
counter = 0
for animal in my_list:
if animal == 'dog':
continue
elif animal == 'cat':
counter += 1
else:
print("Something in here is not a cat or a dog")
return counter
In the above, I first check for 'dog', if it finds 'dog' the loop continues and essentially skips any dogs in the list. If it finds 'cat' it increments the counter by 1. If it finds anything else it reports back "Something in here is not a cat or a dog".
'Continue' isn't a skip keyword, but instead, it tells the loop to keep going if the condition is met. Since I didn't tell Python to do anything other than continue it skips.
ANDY VAN DEN BOS
2,330 PointsANDY VAN DEN BOS
2,330 PointsNicely done.
I was just sitting here questioning my whole progress until this point! I wish what you had just explained was in the lesson somewhere.
Thank you Travis.