首页 > 解决方案 > Python 中 else 语句的语法

问题描述

我对 python 很陌生,我的 else 语句不断出现语法错误。

我将评论语法所在的else 。

print("""
         (A)ddition
         (S)subtraction
         (D)ivision
         (M)multiplication
         """)

operation = input("select an operation from above (initials) = ")

if(operation == "A","S","D","M"):
    
#this is where i am getting syntax
else:
    print("select valid operation.")
        
number1 = int(input("first number = "))
number2 = int(input("second number = "))

if(operation == "A","M","D","S"):

     if operation == "A":
        print("this is the result = ", number1+number2)

    elif operation == "S":
        print("this is the final result", number1 - number2)

    elif operation == "M":
        print("this is the final result", number1 * number2)

    elif operation == "D":
        print("this is the final result", number1/number2, ".And this is the remainder = ",number1&number2)

标签: python

解决方案


您不必缩进 else 语句。

if:

else:

推荐阅读