mercredi 21 septembre 2016

(PYTHON3) Else Statement Always Runs

I've been making a basic calculator with Python and I have come across this issue. After the calculations are made "Invalid Number" always prints and then the pause happens. I think it has something to do with the newline breaking the if block but I'm not sure.

Any help will be appreciated. Thanks in advance.

def badnum():
        print("Invalid Number")
        print("Press enter to continue")
        input("")
def main():
        print("Select an action ")
        print("1.) Add")
        print("2.) Subtract")
        print("3.) Multiply")
        print("4.) Divide")
        ac = int(input(">>>"))
        if ac == 1:
                print("First number :")
                fn = float(input(">>>"))
                print("Second number :")
                sn = float(input(">>>"))
                a = fn+sn
                print(a)
        if ac == 2:
                print("First number :")
                fn = float(input(">>>"))
                print("Second number :")
                sn = float(input(">>>"))
                a = fn-sn
                print(a)
        if ac == 3:
                print("First number :")
                fn = float(input(">>>"))
                print("Second number :")
                sn = float(input(">>>"))
                a = fn*sn
            print(a)
        if ac == 4:
                print("First number :")
                fn = float(input(">>>"))
                print("Second number :")
                sn = float(input(">>>"))
                a = fn/sn
                print(a)
        else:
                badnum()
print("\n"*100)
while True:
    try:
        main()
    except ValueError:
        badnum()
    except ZeroDivisionError:
        print("Infinity")
    print("\n"*100)

Aucun commentaire:

Enregistrer un commentaire