Code works, but ignores 'else'


#1

https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/2/6?curriculum_id=4f89dab3d788890003000096

So, my code works, but it ignores the 'else' statement. It only prints:
"These are the boys in your class: "

I expect it to print "Mario" "Peter"

names = ["Mario", "Katka", "Peter"]

print "These are the boys in your class: "
for n in names:
    if n == "Katka":
        print "She is a girl"
    break
else:
    print "Mario" "Peter"


#2

what do you think break does?


#3

If I dont include break then I get this:
"These are the boys in your class:
She is a girl
MarioPeter"

The break is suppose to tell the program to stop, right?


#4

No, break is to break your loop

If you have for/else, else will only execute when the loop condition becomes false, not when the loop breaks


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.