FAQ: Learn Python: Loops - Break


#1

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!


#2
dog_breeds_available_for_adoption = ['french_bulldog', 'dalmation', 'shihtzu', 'poodle', 'collie']
dog_breed_I_want = 'dalmation'

for adoption in dog_breeds_available_for_adoption:
  ######## This is for Problem 1 ########
  # You have a list of dog breeds[dog_breeds_available_for_adoption] you can adopt[adoption]
  print(adoption)
  # list and print out each dog breed.  Remember, adoption is representing each dog available for adoption.
  # You do not want to print out dog_breeds_available_for_adoption as it will print the list, not the loop.
  ######## This is for Problem 2 ########
  if adoption == dog_breed_I_want:
  # check if (if) it is equal (==) to dog_breed_I_want.  If so, print (print) "They have the dog I want!"
  # This should be very straight forward. again, adoption rep all the breeds in the list.  
  # So it compares the list of breeds to the dog you want.
    print("They have the dog I want!")
  ######## This is for Problem 3 ########
    break
  # This essentially will pump the breaks on the code not to look any further.
  # So it will print all the breeds before it until it gets to the one you want then prints.


#3

Hello everyone!

I have a question regarding indent. Why do we have no indent in the line of print(“They have the dog I want!”)?
And what will happen if we insert indent so that the line aligns with the if statement above it?
Thank you very much!


#4

Because it is not part of the the if block nor part of the for block.


#5
items_on_sale = ["blue_shirt", "striped_socks", "knit_dress", "red_headband", "dinosaur_onesie"]

print("Checking the sale list!")
for item in items_on_sale:
  print(item)
  if item == "knit_dress":
    break
print("End of search!")

I dont understand this: since the programm executes orders from above to below, when theres no command to say stop at this point before it start printing, why didnt the programm just print all the items in the list?I mean why not be like this? The flow makes more sense:

print("Checking the sale list!")
for item in items_on_sale:
  if item == "knit_dress":
    break
    print(items)
print("End of search!")

#6

We cannot see your indentation. I’m going to edit your post to see if any issues become visible…


We can see print after break which is not possible. Anything after break is unreachable.