“elif”据说是无效的语法

时间:2014-11-20 20:46:16

标签: python if-statement python-3.x

这里有些代码由于语句不正确而无法正常工作

#Calculator
print("This is a program that can be used as a calculator")
print("Press 1 to add two numbers")
print("Press 2 to subtract two numbers")
print("Press 3 to divide two numbers")
print("Press 4 to multiply two numbers")

while True:
    Selection=input("What do you want to choice")
    if Selection  in("1"):
        add1=input("Input your first number")
        add2=input("Input your second number")
        resultAdd= int(add1+add2)
        print ("Your result is ", resultAdd)

        elif Selection in("2"):
            subtract1= input("Input your first number")
            subtract2= input("Input your second number")
            resultSubtract= int(subtract1-subtract2)
            print ("Your result is ", resultSubtract)

            elif Selection in("3"):
            divide1= input("Input your first number")
            divide2= input("Input your second number")
            resultDivide= int(divide1/divide2)
            print ("Your result is ",resultDivide)

            elif Selection in("4"):
                multiply1=input("Input your first number")
                multiply2=input("Input your second number")
                resultMultiply= int (multiply1*multiply2)
                print("Your result is ", resultMultilpy)

                break
            else:
                print("Invalid respnse")

我可能错过了一些非常简单但只想知道我做错了什么

1 个答案:

答案 0 :(得分:2)

这是你的对齐:你的elif与首发if不一致。

#Calculator
print("This is a program that can be used as a calculator")
print("Press 1 to add two numbers")
print("Press 2 to subtract two numbers")
print("Press 3 to divide two numbers")
print("Press 4 to multiply two numbers")

while True:
    Selection=input("What do you want to choice")
    if Selection  in("1"):
        add1=input("Input your first number")
        add2=input("Input your second number")
        resultAdd= int(add1+add2)
        print ("Your result is ", resultAdd)

    elif Selection in("2"):
        subtract1= input("Input your first number")
        subtract2= input("Input your second number")
        resultSubtract= int(subtract1-subtract2)
        print ("Your result is ", resultSubtract)

    elif Selection in("3"):
        divide1= input("Input your first number")
        divide2= input("Input your second number")
        resultDivide= int(divide1/divide2)
        print ("Your result is ",resultDivide)

    elif Selection in("4"):
        multiply1=input("Input your first number")
        multiply2=input("Input your second number")
        resultMultiply= int (multiply1*multiply2)
        print("Your result is ", resultMultilpy)

        break
    else:
        print("Invalid respnse")