Else statement in Python 3 always runs

☆樱花仙子☆ 提交于 2021-02-17 05:34:08

问题


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)

回答1:


No, it has got something to do with how you have written your code, consider this with if...elif:

ac = int(input(">>>"))
if ac == 1:
        print("First number :")
        fn = float(input(">>>"))
        print("Second number :")
        sn = float(input(">>>"))
        a = fn+sn
        print(a)
elif ac == 2:
        print("First number :")
        fn = float(input(">>>"))
        print("Second number :")
        sn = float(input(">>>"))
        a = fn-sn
        print(a)
elif ac == 3:
        print("First number :")
        fn = float(input(">>>"))
        print("Second number :")
        sn = float(input(">>>"))
        a = fn*sn
        print(a)
elif ac == 4:
        print("First number :")
        fn = float(input(">>>"))
        print("Second number :")
        sn = float(input(">>>"))
        a = fn/sn
        print(a)
else:
        badnum()


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.


回答2:


You shoud use elif:

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



回答3:


If I understand you correctly, you just need to replace second and further if with elif:

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

And "Invalid Number" will not be printed after each calculation.



来源:https://stackoverflow.com/questions/39616937/else-statement-in-python-3-always-runs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!