这里有些代码由于错误的陈述而无法正常工作

#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")


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

最佳答案

这是您的对齐方式:您的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")

关于python - 据说“elif”是无效的语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27049229/

10-12 21:43