I need to probably rewrite the whole program because I’ve added more error trapping and a fourth textfield. It has increased complexity quite a bit, at least for me. It now cycles through all four textfields, find any two valid fields, decides whether to use Pythagorean Theorem or plain trig, and checks all for errors. One error in particular is that it is possible with the answers to have the short side of the triangle be longer than the long side. Obviously this can never happen so it’s now trapped and alerts the user.
Below is what I now have. I realize my code is a longer than it needs to be and fairly cumbersome at this point but I’m just learning Python and intend to try to rewrite it in a better and more efficient way.
def button3_tapped(sender):
if (v['textfield3'].text)!="" and (v['textfield4'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
hyp = float(v['textfield3'].text)
sht = float(v['textfield4'].text)
hypa = hyp
shta = sht
hyp = pow(hyp,2)
sht = pow(sht,2)
lng = math.sqrt(hyp - sht)
lnga = lng
ang = math.asin(shta/hypa)
ang = math.degrees(ang)
v['textfield5'].text_color = 'green'
v['textfield5'].text = str(lng)
v['textfield6'].text_color = 'green'
v['textfield6'].text = str(ang)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
elif (v['textfield4'].text)!="" and (v['textfield5'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
sht = float(v['textfield4'].text)
shta = sht
lng = float(v['textfield5'].text)
lnga = lng
sht = pow(sht,2)
lng = pow(lng,2)
hyp = math.sqrt(sht + lng)
hypa = hyp
ang = math.asin(shta/hypa)
ang = math.degrees(ang)
v['textfield3'].text_color = 'green'
v['textfield3'].text = str(hyp)
v['textfield6'].text_color = 'green'
v['textfield6'].text = str(ang)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
elif (v['textfield3'].text)!="" and (v['textfield5'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
hyp = float(v['textfield3'].text)
hypa = hyp
lng = float(v['textfield5'].text)
lnga = lng
hyp = pow(hyp,2)
lng = pow(lng,2)
sht = math.sqrt(hyp - lng)
shta = sht
ang = math.asin(shta/hypa)
ang = math.degrees(ang)
v['textfield4'].text_color = 'green'
v['textfield4'].text = str(sht)
v['textfield6'].text_color = 'green'
v['textfield6'].text = str(ang)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
elif (v['textfield3'].text)!="" and (v['textfield6'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
hyp = float(v['textfield3'].text)
ang = float(v['textfield6'].text)
ang = math.radians(ang)
hypa = hyp
sht = hyp * math.sin(ang)
shta = sht
hypc = pow(hyp,2)
shtc = pow(sht,2)
lng = math.sqrt(hypc - shtc)
lnga = lng
v['textfield5'].text_color = 'green'
v['textfield5'].text = str(lng)
v['textfield4'].text_color = 'green'
v['textfield4'].text = str(sht)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
elif (v['textfield4'].text)!="" and (v['textfield6'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
sht = float(v['textfield4'].text)
ang = float(v['textfield6'].text)
ang = math.radians(ang)
shta = sht
hyp = sht / math.sin(ang)
hypa = hyp
hypc = pow(hyp,2)
shtc = pow(sht,2)
lng = math.sqrt(hypc - shtc)
shta = sht
v['textfield5'].text_color = 'green'
v['textfield5'].text = str(lng)
v['textfield3'].text_color = 'green'
v['textfield3'].text = str(hyp)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
elif (v['textfield5'].text)!="" and (v['textfield6'].text)!="":
v['label7'].background_color = 'white'
v['label7'].text_color = 'white'
v['label7'].text = ''
lng = float(v['textfield5'].text)
ang = float(v['textfield6'].text)
ang = math.radians(ang)
lnga = lng
hyp = lng / math.cos(ang)
hypa = hyp
hypc = pow(hyp,2)
lngc = pow(lng,2)
sht = math.sqrt(hypc - lngc)
shta = sht
v['textfield4'].text_color = 'green'
v['textfield4'].text = str(sht)
v['textfield3'].text_color = 'green'
v['textfield3'].text = str(hyp)
shtb = float(v['textfield4'].text)
lngb = float(v['textfield5'].text)
if shtb > lngb:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR. Short side cannot be longer than long side.'
else:
v['label7'].background_color = 'red'
v['label7'].text_color = 'white'
v['label7'].text = 'ERROR' ```