# If statement syntax error browsing

• posted
0

``````def button3_tapped(sender):
sidea = float(v['textfield3'].text)I
sideb = float(v['textfield4'].text)
sidec = float(v['textfield5'].text)
if sidea is not None and sideb is not None
sidec = sidea + sideb
v['label1'].text = 'sidec = ' + str(sidec)
``````

I have a dialog box with three text fields and a button. Upon pushing the button I want to look at the three fields and see if they’re blank. If two are not then do something. I keep getting a syntax error on the above if statement and can’t figure out why.
I also tried if (sidea.length()!=0)

• posted
0

@ccc

Below will calculate short and long with given hypotenuse and angle but you have to enter zero for short and long input.

``````import math

def triangle(short, long, hypotenuse, angle):
pass
if hypotenuse and angle:
print ('Short = ' + str(hypotenuse * (math.sin(angle))))
print ('Long = ' + str(hypotenuse * (math.cos(angle))))

triangle(0,0,10,30)
``````

• posted
0

@ccc

This will calculate any side or angle. But input still has to be zero for unknowns at input.

``````import math

def triangle(short, long, hypotenuse, angle):
pass
if len([x for x in (hypotenuse, short, long, angle) if x]) == 2:
if hypotenuse and angle:
print   ('Short = ' + str(hypotenuse * (math.sin(angle))))
print   ('Long = ' + str(hypotenuse * (math.cos(angle))))
elif long and angle:
print   ('Short = ' + str(long * (math.tan(angle))))
print   ('Hypotnuse = ' + str(long / (math.cos(angle))))
elif short and angle:
print   ('Long = ' + str(short / (math.tan(angle))))
print   ('Hypotnuse = ' + str(short / (math.sin(angle))))
elif hypotenuse and short:
print ('Long = ' + str(math.sqrt(hypotenuse ** 2 - short ** 2)))
print ('Angle = ' + str(math.degrees(math.asin(short / hypotenuse))))
elif hypotenuse and long:
print ('Short = ' + str(math.sqrt(hypotenuse ** 2 - long ** 2)))
print ('Angle = ' + str(math.degrees(math.acos(long / hypotenuse))))
elif short and long:
print ('Hypotenuse = ' + str(math.sqrt(short ** 2 + long ** 2)))
print ('Angle = ' + str(math.degrees(math.atan(short / long))))
else:
print ("ERROR")

triangle(5,0,10,0)
``````

• posted
0

Step 0: drop the `pass` line.
Step 1:

``````    if len([x for x in (hypotenuse, short, long, angle) if x]) != 2:
raise ValueError("Exactly two parameters must be zero.")
``````

Step 2: change `triangle()` so it never prints but instead returns the answer with the statement `return short, long, hypotenuse, angle`
Step 3: change `triangle(5,0,10,0)` to `print(triangle(5,0,10,0))`.

Step 1: is called a fast-fail which is safer and more readable than failing later in the function. Basically is says "I see a bad situation (bad input data) so I am going to raise an exception and stop processing".

• posted
0

@ccc, I am sure I just saw you using `count` somewhere:

``````if (hyp, ...).count(0) != 2:
raise ...``````

• posted
0

@ccc

I don’t understand step 2

Step 2: change triangle() so it never prints but instead returns the answer with the statement return short, long, hypotenuse, angle.

Are you wanting me to change....
def triangle(short, long, hypotenuse, angle):

Also, changing to print(triangle(5,0,10,0)) adds “none” after returned values

• posted
0

``````def triangle(short, long, hypotenuse, angle):
# Your code contains no print() calls.
return short, long, hypotenuse, angle
``````

• posted
0

Your code contains no print() calls, like

``````        if hypotenuse and angle:
short = hypotenuse * math.sin(angle)
long  = hypotenuse * math.cos(angle)
``````

• posted
0

@cvp

I still don’t know what you’re wanting. My code has many print calls.
Adding return short, long, hypotenuse, angle where you have it above won’t do anything because it’s part of the last elif. If I add it in line with def Tringle it errors.

``````import math

def triangle(short, long, hypotenuse, angle):
pass
if len([x for x in (hypotenuse, short, long, angle) if x]) == 2:
if hypotenuse and angle:
print   ('Short = ' + str(hypotenuse * (math.sin(angle))))
print   ('Long = ' + str(hypotenuse * (math.cos(angle))))
elif long and angle:
print   ('Short = ' + str(long * (math.tan(angle))))
print   ('Hypotnuse = ' + str(long / (math.cos(angle))))
elif short and angle:
print   ('Long = ' + str(short / (math.tan(angle))))
print   ('Hypotnuse = ' + str(short / (math.sin(angle))))
elif hypotenuse and short:
print ('Long = ' + str(math.sqrt(hypotenuse ** 2 - short ** 2)))
print ('Angle = ' + str(math.degrees(math.asin(short / hypotenuse))))
elif hypotenuse and long:
print ('Short = ' + str(math.sqrt(hypotenuse ** 2 - long ** 2)))
print ('Angle = ' + str(math.degrees(math.acos(long / hypotenuse))))
elif short and long:
print ('Hypotenuse = ' + str(math.sqrt(short ** 2 + long ** 2)))
print ('Angle = ' + str(math.degrees(math.atan(short / long))))
else:
print ("ERROR")

triangle(5,0,10,0)
``````

• posted
0

@OkieWolf I understand, I only want to show what @ccc adviced. The triangle code without any print and only one print of triangle results

• posted
0

@cvp

I think I understand now. But I can’t get the indentation at “return” to stop erroring.

``````import math

def triangle(short, long, hypotenuse, angle):
if len([x for x in (hypotenuse, short, long, angle) if x]) != 2:
raise ValueError("Exactly two parameters must be zero.")
if hypotenuse and angle:
print   ('Short = ' + str(hypotenuse * (math.sin(angle))))
print   ('Long = ' + str(hypotenuse * (math.cos(angle))))
elif long and angle:
print   ('Short = ' + str(long * (math.tan(angle))))
print   ('Hypotnuse = ' + str(long / (math.cos(angle))))
elif short and angle:
print   ('Long = ' + str(short / (math.tan(angle))))
print   ('Hypotnuse = ' + str(short / (math.sin(angle))))
elif hypotenuse and short:
print ('Long = ' + str(math.sqrt(hypotenuse ** 2 - short ** 2)))
print ('Angle = ' + str(math.degrees(math.asin(short / hypotenuse))))
elif hypotenuse and long:
print ('Short = ' + str(math.sqrt(hypotenuse ** 2 - long ** 2)))
print ('Angle = ' + str(math.degrees(math.acos(long / hypotenuse))))
elif short and long:
print ('Hypotenuse = ' + str(math.sqrt(short ** 2 + long ** 2)))
print ('Angle = ' + str(math.degrees(math.atan(short / long))))
return(short, long, hypotenuse, angle)

#triangle(5,0,10,0)
``````

• posted
1

``````from math import acos, asin, atan, cos, degrees, radians, sin, sqrt, tan

def triangle(short, long, hypotenuse=0, angle=0):
if (hypotenuse, short, long, angle).count(0) != 2:
raise ValueError("Exactly two parameters must be zero.")
if short:
if long:
hypotenuse = sqrt(short ** 2 + long ** 2)
angle = degrees(atan(short / long))
elif hypotenuse:
long = sqrt(hypotenuse ** 2 - short ** 2)
angle = degrees(asin(short / hypotenuse))
else:  # short and angle
elif long:
if hypotenuse:
short = sqrt(hypotenuse ** 2 - long ** 2)
angle = degrees(acos(long / hypotenuse))
else:  # long and angle
else:  # hypotenuse and angle
short, long, hypotenuse = sorted([short, long, hypotenuse])
return float(short), float(long), float(hypotenuse), float(angle)

# The result for each of these should be (3.0, 4.0, 5.0, 36.8698976458440x)
print(triangle(3, 4))  # short and long
print(triangle(3, 0, 5))  # short and hypotenuse
print(triangle(0, 4, 5))  # long and hypotenuse
print(triangle(0, 0, 5, 36.86989764584402))  # hypotenuse and angle
``````

• posted
0

This post is deleted!

• posted
0

@ccc as short and long are différent, why do you think angle could be 45°?

• posted
0

``````    else:  # hypotenuse and angle
``````

• posted
0

Thanks! Now you know why I failed trig back in the day...

Both issues fixed in the code above.

• posted
0

For completeness...

``````def button3_tapped(sender):
hypotenuse = float(v['hypotenuse'].text or 0)
short = float(v['short'].text or 0)
long = float(v['long'].text or 0)
angle = float(v['angle'].text or 0)
try:
short, long, hypotenuse, angle = triangle(short, long, hypotenuse, angle)
except ValueError:
v['mess'].text = "Enter two fields"
v['mess'].background_color = 'red'
v['mess'].text_color = 'white'
return
v['short'].text = str(short)
v['long'].text = str(long)
v['hypotenuse'].text = str(hypotenuse)
v['angle'].text = str(angle)
``````

• posted
1

@ccc said:

For completeness..

``````def button3_tapped(sender):
# erase previous eventual error message
v['mess'].text = ""
v['mess'].background_color = 'white'
.
.
.
``````

Internal error.

Oops! Looks like something went wrong!