Else statement in Python 3 always runs

Solution 1:

It has got something to do with how you have structured your code, consider this with if...elif:

print("Select an action ")
print("1.) Add")
print("2.) Subtract")
print("3.) Multiply")
print("4.) Divide")
ac = int(input(">>>"))
print("First number :")
fn = float(input(">>>"))
print("Second number :")
sn = float(input(">>>"))

if ac == 1:
    print(fn + sn)
elif ac == 2:
    print(fn - sn)
elif ac == 3:
    print(fn * sn)
elif ac == 4:
    print(fn / sn)
else:
    print("Invalid Number")
    print("Press enter to continue")
    input()

Explanation: Before, you were checking for ac == 1 and ac == 4 which cannot both be true, so the second else statement was executed as well. This can be omitted with the if..elif construction: once, one of the earlier comparisons become true, the rest is not executed anymore.

Solution 2:

With Python 3.10+ you can use the match statement:

match ac:
    case 1:
        ...
    case 2:
        ...
    case 3:
        ...
    case 4:
        ...
    case _:  # default
        ...

Before Python 3.10

You shoud use elif:

if ac == 1:
    ...
elif ac == 2:
    ...
elif ac == 3:
    ...
elif ac == 4:
    ...
else:
    ...