# Invalid syntax on if statement

**URL:** <https://discuss.python.org/t/invalid-syntax-on-if-statement/6078>\
**Category:** Python Help\
**Created:** [December 8, 2020, 7:33pm UTC](https://discuss.python.org/t/invalid-syntax-on-if-statement/6078 "2020-12-08T19:33:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![james](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@james](https://discuss.python.org/u/james)\
**Post date:** [December 8, 2020, 7:33pm UTC](https://discuss.python.org/t/invalid-syntax-on-if-statement/6078/1 "2020-12-08T19:33:23Z")

</div>

So I am making a calculator in python as part of a school homework project and while I am aware it is not quite finished, I have come across an invalid syntax in my code on line 22. it is saying that the bracket on this line is an invalid syntax. Can someone help me out with this please? The rest I should be able to do myself.

print(“Calculator”)  
print(" ")

def Add(a,b):  
return a + b

def Minus(a,b):  
return a - b

def Divide(a,b):  
return a / b

def Multiply(a,b):  
return a \* b

while True:  
result =0  
value1 =0.0  
while True:  
value1 = float(input("value: "))  
operator = chr(input("operator: ")  
if value1 == 0.0:  
print(result)  
if operator == ‘+’:  
result += Add(result,value1)  
if operator == ‘-’:  
result -= Minus(result,value1)  
if operator == ‘/’:  
result /= Divide(result,value1)  
if operator == ‘\*’:  
result \*= Multiply(result,value1)  
else:  
print(result)

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [December 8, 2020, 10:14pm UTC](https://discuss.python.org/t/invalid-syntax-on-if-statement/6078/2 "2020-12-08T22:14:52Z")

</div>

> So I am making a calculator in python as part of a school homework  
> project and while I am aware it is not quite finished, I have come  
> across an invalid syntax in my code on line 22. it is saying that the  
> bracket on this line is an invalid syntax. Can someone help me out with  
> this please? The rest I should be able to do myself.  
> […]

It would be helpful if you also pasted in the entire error message. That  
will usually pinpoint exactly where Python was unhappy. (Occasionally  
the actual error is earlier in the code, but regardless the rrror  
message is important in finding the mistake.)

That said, your if-statements look syntacticly ok to my eye.

I would remark that for reasons of getting the logic correct you may  
want an else: after the first if, and elifs instead of ifs for the  
second and following operator tests.

> while True:  
> result =0  
> value1 =0.0  
> while True:  
> value1 = float(input("value: "))  
> operator = chr(input("operator: ")  
> if value1 == 0.0:  
> print(result)  
> if operator == ‘+’:  
> result += Add(result,value1)  
> if operator == ‘-’:  
> result -= Minus(result,value1)  
> if operator == ‘/’:  
> result /= Divide(result,value1)  
> if operator == ‘\*’:  
> result \*= Multiply(result,value1)  
> else:  
> print(result)

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [December 8, 2020, 10:51pm UTC](https://discuss.python.org/t/invalid-syntax-on-if-statement/6078/3 "2020-12-08T22:51:45Z")

</div>

Sometimes the interpreter can’t tell where the syntax error is until it  
reaches the next line, especially errors involving brackets. This is one  
of those cases. You have:

```
operator = chr(input("operator: ")
if value1 == 0.0:
    ...

```

Notice the call to `chr()` has two opening round brackets but only one  
closing bracket. The interpreter can’t tell that’s wrong until it  
reaches the “if” on the next line, so that’s where it reports the error.
