If statement syntax error
-
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)
-
@OkieWolf I understand, I only want to show what @ccc adviced. The triangle code without any print and only one print of triangle results
-
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!last edited by ccc
-
@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' . . .