-2

I am very new to python and my else statement keeps on getting a syntax error.

I'll comment the else where the syntax is.

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)
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • Write `if(operation in ["A","S","D","M"]):` – felice Sep 29 '20 at 14:40
  • 4
    Please use code formatting to reproduce your code with its actual indentation. Python is indentation-sensitive. Please also include the traceback, formatted as code, as well. It should include a caret that points to the character at which the Syntax can no longer be reconstructed. – MisterMiyagi Sep 29 '20 at 14:40
  • 1
    Note that an else only makes sense following an `if` *and its body*. In the code, the first `else` has neither. When should it actually trigger? – MisterMiyagi Sep 29 '20 at 14:43
  • 1
    Related link for what is I think you are trying to do: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Tomerikoo Sep 29 '20 at 14:47
  • @Tomerikoo actually i want it to check first whether the input is correct and if it is not then i want it stop it right there. what i wrote after that else is basically 2nd part of my problem, u can think it like that. – Karan Modi Sep 29 '20 at 15:10
  • 1
    Please do *not* include your code/error as image only! Use a code block to allow people to easily copy/paste the code and fix it. – MisterMiyagi Sep 29 '20 at 15:20
  • @MisterMiyagi sorry about it – Karan Modi Sep 29 '20 at 15:22
  • 1
    Please take a look at the [ask] page. In specific, do *not* shift the topic of your question via edits after you have received viable answers. This will invalidate existing answers, and worst case lead to people penalising the answerers for the perceived off-topic answers. If your original question about the syntax errors has been answered but you encounter a new issue, then a) *accept* the answer that solved your initial problem and b) as a *new* question for the new problem. – MisterMiyagi Sep 29 '20 at 15:29

3 Answers3

1

you do not have to indent the else sentece.

if:

else:
JuanTargon
  • 11
  • 1
  • 1
    But then the `if:` will be empty and that's just another syntax error... – Tomerikoo Sep 29 '20 at 14:44
  • yes, i know. you need to have some code after every if/else sentece. youre right – JuanTargon Sep 29 '20 at 14:49
  • 1
    But OP has no code after the `if`, so I don't see how that suggestion is helpful... – Tomerikoo Sep 29 '20 at 14:57
  • But I want to print nothing after if. I just want it to check whether the input is correct or no. If it is not then i want the program to stop from going on forward. – Karan Modi Sep 29 '20 at 15:00
  • 1
    @KaranModi so you should instead reverse the logic: `if operation not in ["A","S","D","M"]: print("select valid operation.")`. Notice the `not` in there. You can't have an empty `if`, you can put `pass` but that's just pointless – Tomerikoo Sep 29 '20 at 15:07
  • @Tomerikoo is right. you can also use ```pass``` if you want to do nothig in the if statement, but using ```not``` is the best option – JuanTargon Sep 29 '20 at 15:15
  • @Tomerikoo ok I got it what u were saying and I tried it but the program doesn't stop when I type wrong input. it goes on to ask me the numbers. – Karan Modi Sep 29 '20 at 15:21
  • @KaranModi please look at the answer I posted and if you have problems comment there – Tomerikoo Sep 29 '20 at 15:21
0

You need to put something in the if statement above and the else statement should be at the same indentation.

Kareem
  • 11
  • 1
  • But I want to print nothing after if. I just want it to check whether the input is correct or no. If it is not then i want the program to stop from going on forward. – Karan Modi Sep 29 '20 at 15:01
0

As others have pointed out, you can't have a blank space after an if or an else.

Since you only want the else part, you have to negate the if criteria, so it becomes the if part.

So:

if(operation == "A","S","D","M"):
    do_nothing = True
else:
    print("select valid operation.")

becomes:

if operation not in ("A","S","D","M"):
    print("select valid operation.")

(And yes, your == isn't going to work either on a list like that, you need in)

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • What terminal? This form won't give you an error. Have you fixed both your `if` points that use `==` to use `in` on a tuple instead? – michjnich Sep 29 '20 at 17:03
  • i am new to python and have learn till conditional statements only. I have no idea what tuple is. – Karan Modi Sep 29 '20 at 19:15