Code works, but ignores 'else'


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"
    print "Mario" "Peter"


what do you think break does?


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

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


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


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