Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
If statement syntax error

This was an invitation for you or someone else to write a function that can take between 2 and 4 parameters and calculate all other variables or raise an exception if there is no possible solution. This function would allow us to isolate all UI operations from all math operations. It would make it easier to test our math operations without the UI and would allow us to easily port the math operations to a different platform.

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: angle = math.radians(angle) print ('Short = ' + str(hypotenuse * (math.sin(angle)))) print ('Long = ' + str(hypotenuse * (math.cos(angle)))) triangle(0,0,10,30)

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: angle = math.radians(angle) print ('Short = ' + str(hypotenuse * (math.sin(angle)))) print ('Long = ' + str(hypotenuse * (math.cos(angle)))) elif long and angle: angle = math.radians(angle) print ('Short = ' + str(long * (math.tan(angle)))) print ('Hypotnuse = ' + str(long / (math.cos(angle)))) elif short and angle: angle = math.radians(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)

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 statementreturn short, long, hypotenuse, angle
Step 3: changetriangle(5,0,10,0)
toprint(triangle(5,0,10,0))
.Step 1: is called a fastfail 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".

@ccc, I am sure I just saw you using
count
somewhere:if (hyp, ...).count(0) != 2: raise ...

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

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

Your code contains no print() calls, like
if hypotenuse and angle: angle = math.radians(angle) short = hypotenuse * math.sin(angle) long = hypotenuse * math.cos(angle)

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: angle = math.radians(angle) print ('Short = ' + str(hypotenuse * (math.sin(angle)))) print ('Long = ' + str(hypotenuse * (math.cos(angle)))) elif long and angle: angle = math.radians(angle) print ('Short = ' + str(long * (math.tan(angle)))) print ('Hypotnuse = ' + str(long / (math.cos(angle)))) elif short and angle: angle = math.radians(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)


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: angle = math.radians(angle) print ('Short = ' + str(hypotenuse * (math.sin(angle)))) print ('Long = ' + str(hypotenuse * (math.cos(angle)))) elif long and angle: angle = math.radians(angle) print ('Short = ' + str(long * (math.tan(angle)))) print ('Hypotnuse = ' + str(long / (math.cos(angle)))) elif short and angle: angle = math.radians(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)

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.") radians_angle = radians(angle) 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 long = short / tan(radians_angle) hypotenuse = short / sin(radians_angle) elif long: if hypotenuse: short = sqrt(hypotenuse ** 2  long ** 2) angle = degrees(acos(long / hypotenuse)) else: # long and angle short = long * tan(radians_angle) hypotenuse = long / cos(radians_angle) else: # hypotenuse and angle short = hypotenuse * sin(radians_angle) long = hypotenuse * cos(radians_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

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

@ccc error, you use angle instead of radians_angle
else: # hypotenuse and angle short = hypotenuse * sin(radians_angle) long = hypotenuse * cos(radians_angle)

Thanks! Now you know why I failed trig back in the day...
Both issues fixed in the code above.

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)

@ccc said:
For completeness..
def button3_tapped(sender): # erase previous eventual error message v['mess'].text = "" v['mess'].background_color = 'white' . . .