FAQ: Learn Python: Loops - Break

This community-built FAQ covers the “Break” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Break

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: My solution

4 posts were split to a new topic: Why does my print statement not need to be indented?

5 posts were split to a new topic: Where is my indentation error?

2 posts were split to a new topic: Can I use this code to check for two dogs I want?

i do not know why it is incorrect although my answer is the same as it is expected. is there someone can explain this?

You have a typo in the string you want to print out. You have “They hava the dog I want!” when it should be “They have the dog I want!”

I believe that it is technically correct but the lesson is expecting what it was told to look for.

All the time the same. Very frustrating. Is it any way you check the output instead?

The example on the right follows a general convention…

our_target_object == comparison_object

In this case, our target object is denoted by the parameter.

No difference really, apart from generalized mindset. Certain norms are very well established in the community. We do well to learn and follow them so they become cemented in our thought process.

1 Like


Hi guys!
Why there is “french_bulldog” printed when we just try to find “dalmatian”?

1 Like

When trying to see if an item is contained in a list is it more efficient to loop through it as the example does in this lesson or is it more efficient to do an if statement like if var.count({item we’re looking for}) > 0 ?

Looping and counting are two ways; another is a membership test using in.

a = 'superstar`
print ('s' in a)    #  True
if s in a: print (a.count('s'))    #  2

The same can be applied to a list, tuple or set.

Thanks for the response! Is one of them more efficient than the others depending on the size of the dataset or would it come down to preference?

1 Like

If all we want is confirm membership, in is the most efficient, one could suppose. However, it is quite like an iterator, as is count() so as time is concerned they may be roughly the same speed, at least in terms of complexity, O(N). Either would be faster than a loop that examines each character.

1 Like

Are there other use cases for the break command outside of loops?

Had the same exact question. Same thing happened to me. Looks like it only breaks after your dog_breed_I_want choice. But how do we make it only break at that choice and not have to cycle through the beginning and have that get printed. End print result makes zero sense…

I don’t quite follow your point of confusion. Perhaps you can elaborate on your reasoning.

dog_breeds_available_for_adoption is a list of strings.

The value assigned to the variable dog_breed_I_want is the string 'dalmatian'.

for dog_breed in dog_breeds_available_for_adoption:
    print(dog_breed)
    if dog_breed == dog_breed_I_want:
        print("They have the dog I want!")
        break
  • First iteration of loop. The first element of the list i.e. the string 'french_bulldog' is assigned to the loop variable dog_breed. The first statement of the loop i.e. print(dog_breed) prints this string. The if condition evaluates to False as 'french_bulldog''dalmatian'. The iteration finishes.
  • Second iteration of loop. The second element of the list i.e. the string 'dalmatian' is assigned to the loop variable dog_breed. The first statement of the loop i.e. print(dog_breed) prints this string. The if condition evaluates to True as 'dalmatian' == 'dalmatian'. The string "They have the dog I want!" is printed and then the break statement ends the loop.

Suppose the value assigned to the variable dog_breed_I_want was the string 'sheltie'. Then the for loop would print all the breeds in the list dog_breeds_available_for_adoption. Since 'sheltie' is not an element of this list, so the if condition never evaluates to True (Neither the string "They have the dog I want!" is printed Nor the break statement is reached). When the loop has iterated over all the strings in the list, the loop ends.

That was the whole reason I came to forums. I was like, If
dog_breed_I_want == “Dalmnation”, it should ONLY be printing Dalmnation.

Nevermind, I figured out why it’s printing the french bulldog.

dog_breeds_available_for_adoption = ["french_bulldog", "dalmatian", "shihtzu", "poodle", "collie"]
dog_breed_I_want = "dalmatian"

for dog_breed in dog_breeds_available_for_adoption:
  print(dog_breed)
  if dog_breed == dog_breed_I_want:
    print("They have the dog I want!")
    break

The reason it’s printing French Bulldog is because of the first print(dog_breed) statement. Its going to print out French_Bulldog, then continue with the rest of the script. In this scenario, stopping at Dalmation. Thus having both dogs printed on the console.

A post was split to a new topic: Sorting Long Lists