omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    7
    53
    11311
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • OkieWolf
      OkieWolf last edited by ccc

      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)

      mcriley821 1 Reply Last reply Reply Quote 0
      • mcriley821
        mcriley821 @OkieWolf last edited by mcriley821

        @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 statement

        Like so

        if sidea and sideb:#colon right here
            #do stuff
        
        1 Reply Last reply Reply Quote 1
        • stephen
          stephen last edited by

          @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!

          ```

          1 Reply Last reply Reply Quote 1
          • OkieWolf
            OkieWolf last edited by OkieWolf

            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' `
            mcriley821 1 Reply Last reply Reply Quote 0
            • mcriley821
              mcriley821 @OkieWolf last edited by

              @OkieWolf

              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}'
              
              1 Reply Last reply Reply Quote 0
              • OkieWolf
                OkieWolf last edited by

                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' ```
                mcriley821 1 Reply Last reply Reply Quote 0
                • mcriley821
                  mcriley821 @OkieWolf last edited by

                  @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

                  1 Reply Last reply Reply Quote 1
                  • OkieWolf
                    OkieWolf last edited by

                    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' ```
                    mcriley821 1 Reply Last reply Reply Quote 0
                    • mcriley821
                      mcriley821 @OkieWolf last edited by mcriley821

                      @OkieWolf
                      To specify hex you have to have a # in front

                      red = '#ff0000'
                      

                      The current hex value you have is mostly white. It’s also too long. 'red' should work too

                      OkieWolf 1 Reply Last reply Reply Quote 0
                      • OkieWolf
                        OkieWolf @mcriley821 last edited by

                        @mcriley821
                        Still doesn’t change

                        mcriley821 1 Reply Last reply Reply Quote 0
                        • mcriley821
                          mcriley821 @OkieWolf last edited by

                          @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?

                          OkieWolf 1 Reply Last reply Reply Quote 0
                          • OkieWolf
                            OkieWolf @mcriley821 last edited by

                            @mcriley821
                            It’s getting to the else statement because it’s inputting the text “Enter All Fields”. It just won’t change the color.

                            cvp mikael 2 Replies Last reply Reply Quote 0
                            • mcriley821
                              mcriley821 last edited by

                              @OkieWolf

                              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

                              1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @OkieWolf last edited by

                                @OkieWolf already done here

                                1 Reply Last reply Reply Quote 1
                                • mikael
                                  mikael @OkieWolf last edited by

                                  @OkieWolf, you hit one of known quirks. You can set bordered to False, after which background_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))
                                  
                                  OkieWolf 1 Reply Last reply Reply Quote 0
                                  • OkieWolf
                                    OkieWolf last edited by

                                    What about text color? Can I change the textfield text color to red?

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @OkieWolf last edited by

                                      @OkieWolf .text_color = 'red'

                                      1 Reply Last reply Reply Quote 0
                                      • OkieWolf
                                        OkieWolf @mikael last edited by

                                        This post is deleted!
                                        1 Reply Last reply Reply Quote 0
                                        • OkieWolf
                                          OkieWolf last edited by OkieWolf

                                          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' ```
                                          1 Reply Last reply Reply Quote 1
                                          • ccc
                                            ccc last edited by ccc

                                            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'
                                            
                                            OkieWolf 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors