'understaing nested for loop

I am just testing a code that i have written but cant figure out why theloop is getting executed twice for the 3 and 4 th value in the array and 4 times for the 5 and 6th value.

My code is as follow

Angry=["irritated","annoying"]
Sad=["upset","scared"]
Happy=["awsome","excited"]
a=[]
a.extend(Angry)
a.extend(Sad)
a.extend(Happy)


name=input("what is your name")
age=input("whai is your age")
m=input(f"how do you feel today ? choose from the list above {print(a)}")

for i in Angry:
    for j in Sad:
        for k in Happy:
#             print("m is:", m)
#             print("i is:", i)
#             print("j is:", j)
#             print("k is:", k)
            if (m == k):
                print(name,"is feeling HAPPY")
                break
            else:
                continue
        if m == j:
            print(name,"mood is SAD")
            print("m",m)
            print("i",m)
            print("j",m)
            print("k",m)
            break
        else:
            continue
    if m == i:
        print(name,"mood is ANGRY")
        break


Solution 1:[1]

Because break only applies to the inmost loop and not the outer ones. In case of m == k you find that name is happy 2 times 2 times, because you don't break the outer loops.

The nested for loops does this:

For each value in Angry, check each value in Sad and for each value in Sad check each value in Happy:

irritated -> upset -> awesome -> excited -> scared -> awesome -> excited -> annoying -> upset -> awesome -> excited -> scared -> awesome -> excited

Which means each value in Happy is checked 4 times, each value in Sad is checked two times and each value in Angry is checked once.

I take it this was a case study for nested loops, but here's an approach to solve your task differently:

if m in Angry:
    print(name, "mood is ANGRY")
elif m in Sad:
    print(name, "mood is SAD")
elif m in Happy:
    print(name, "is feeling HAPPY")
else:
    print('Unknown feeling?!')

m in Angry checks, well, if m is equal to any element in Angry. The chained if/elif/else cases make sure that m is only found once.

Edit:

If want to keep the nested for loops, you need to break out of outer loops either with a return statement after putting the loop inside a function

def get_message():
    for i in Angry:
        for j in Sad:
            for k in Happy:
                if m == k:
                    print(name, "is feeling HAPPY")
                    return
            if m == j:
                print(name, "mood is SAD")
                return
        if m == i:
            print(name, "mood is ANGRY")
            return


get_message()

or you need to maintain a variable that gets passed as break condition through the loops:

found = False
for i in Angry:
    for j in Sad:
        for k in Happy:
            if m == k:
                print(name, "is feeling HAPPY")
                found = True
            if found:
                break
        if m == j:
            print(name, "mood is SAD")
            found = True
        if found:
            break
    if m == i:
        print(name, "mood is ANGRY")
        found = True
    if found:
        break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1