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
-
@OkieWolf
try to format your code with the </> button, but from what I can tell, it looks like you’re missing a colon ':' after the conditions of the if statementLike so
if sidea and sideb:#colon right here #do stuff
-
@OkieWolf said:
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)def button3_tapped(sender): sidea = float(v['textfield3'].text)I # <= remove "I" typo sideb = float(v['textfield4'].text) sidec = float(v['textfield5'].text) # checking if not None if sidea and sideb: # <= you forgot your ":" statment closing sidec = sidea + sideb v['label1'].text = f'sidec = {sidec}' # changed to fstring
Also remember to use code format on posts 😀 thnk you!
-
The below works. But the if statement isn’t catching the blank textfield. If one is blank it errors trying to create sidea or sideb (whichever is blank). Also, it never gets to the else statement.
def button3_tapped(sender): if (v['textfield3'].text) is not None and (v['textfield4'].text) is not None: sidea = float(v['textfield3'].text) sideb = float(v['textfield4'].text) sidec = sidea + sideb v['textfield5'].text = 'sidec = ' + str(sidec) else: v['textfield5'].text = 'ENTER ALL REQUIRED FIELDS' `
-
def button3_tapped(sender): if v['textfield3'].text and v['textfield4'].text: try: sidea = float(v['textfield3'].text) sideb = float(v['textfield4'].text) except ValueError: v['textfield5'].text = 'ENTER ALL REQUIRED FIELDS CORRECTLY' else: sidec = sidea + sideb v['textfield5'].text = f'sidec = {sidec}'
-
Figured it out. See below....
def button3_tapped(sender): if (v['textfield3'].text)!="" and (v['textfield4'].text)!="": sidea = float(v['textfield3'].text) sideb = float(v['textfield4'].text) sidec = sidea + sideb v['textfield5'].text = 'sidec = ' + str(sidec) else: v['textfield5'].text = 'ENTER ALL REQUIRED FIELDS' ```
-
@OkieWolf this will throw an exception if the string can’t convert to a float. If you suspect that won’t happen, your code is fine. Otherwise my previous code would be more robust
-
New issue.
I want to change the background color of the textfield to red if one of the fields is blank. Below is what I’ve done. It doesn’t error but it doesn’t change the color either. I also tried ‘red’ instead of the hex code but that doesn’t work either.def button3_tapped(sender): if (v['textfield3'].text)!="" and (v['textfield4'].text)!="": sidea = float(v['textfield3'].text) sideb = float(v['textfield4'].text) sidec = sidea + sideb v['textfield5'].text = 'sidec = ' + str(sidec) else: v['textfield5'].background_color = 'fffffff8' v['textfield5'].text = 'ENTER ALL FIELDS' ```
-
@OkieWolf
To specify hex you have to have a # in frontred = '#ff0000'
The current hex value you have is mostly white. It’s also too long. 'red' should work too
-
@mcriley821
Still doesn’t change -
@OkieWolf then it’s not making it to the else statement. What are you inputting to the textfields? Maybe one of them is white space?
-
@mcriley821
It’s getting to the else statement because it’s inputting the text “Enter All Fields”. It just won’t change the color. -
Scratch that, sorry. I was thinking of a label. A textField doesn’t really have a background. It’s background is behind the white layer. What you could do instead is make a red border.
If you really want the whole thing red, I can write a small bit of code with objc_util
-
-
@OkieWolf, you hit one of known quirks. You can set
bordered
to False, after whichbackground_color
works. Or if that appearance does not work for you, @cvp’s solution can be turned into a helper function that you can call with just'red'
:def textfield_background(tf, color): import objc_util tfo = tf.objc_instance.textField() tfo.backgroundColor = objc_util.UIColor.colorWithRed_green_blue_alpha_( *ui.parse_color(color))
-
What about text color? Can I change the textfield text color to red?
-
@OkieWolf .text_color = 'red'
-
This post is deleted! -
Here’s the completed button using the Pythagorean Theorem to calculate. It looks to see which of the three textfields is blank then calculates what it should be based on the input of the other two.
def button3_tapped(sender): if (v['textfield3'].text)!="" and (v['textfield4'].text)!="": hyp = float(v['textfield3'].text) sht = float(v['textfield4'].text) hyp = pow(hyp,2) sht = pow(sht,2) lng = math.sqrt(hyp - sht) v['textfield5'].text_color = 'green' v['textfield5'].text = str(lng) elif (v['textfield4'].text)!="" and (v['textfield5'].text)!="": sht = float(v['textfield4'].text) lng = float(v['textfield5'].text) sht = pow(sht,2) lng = pow(lng,2) hyp = math.sqrt(sht + lng) v['textfield3'].text_color = 'green' v['textfield3'].text = str(hyp) elif (v['textfield3'].text)!="" and (v['textfield5'].text)!="": hyp = float(v['textfield3'].text) lng = float(v['textfield5'].text) hyp = pow(hyp,2) lng = pow(lng,2) sht = math.sqrt(hyp - lng) v['textfield4'].text_color = 'green' v['textfield4'].text = str(sht) else: v['textfield5'].text_color = 'red' v['textfield5'].text = 'ENTER ALL FIELDS' ```
-
The logic is easier to follow if field names describe their content.
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) if len([x for x in (hypotenuse, short, long) if x]) != 2: v['long'].text = "Enter floats in 2 fields" v['long'].text_color = 'red' return if hypotenuse: if short and not long: v['long'].text = str(math.sqrt(hypotenuse ** 2 - short ** 2)) v['long'].text_color = 'green' elif long and not short: v['short'].txt = str(math.sqrt(hypotenuse ** 2 - long ** 2)) v['short'].text_color = 'green' else: v['hypotenuse'].text = str(math.sqrt(short ** 2 + long ** 2)) v['hypotenuse'].text_color = 'green'
-
@ccc
Could you please explain how this works in more detail?
I realize you’ve renamed textfields but I’m not exactly understanding how the code is working. I’m mainly not understanding what’s happening with the or 0 and then the if len([x for x in (hypotenuse, short, long) if x]) != 2hypotenuse = float(v['hypotenuse'].text or 0) short = float(v['short'].text or 0) long = float(v['long'].text or 0) if len([x for x in (hypotenuse, short, long) if x]) != 2: ```