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
    11320
    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.
    • 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
                                    • OkieWolf
                                      OkieWolf @ccc last edited by OkieWolf

                                      @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]) != 2

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

                                        @OkieWolf

                                        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['short'].text or 0 will choose 0 if the text is "". You won’t hit an exception for a blank textfield. You will get an error if the text is alphabetical though.

                                        (hypotenuse,short,long) is a tuple and [x for x in (hypotenuse,short,long) if x] is a list comprehension that steps through the tuple
                                        and makes a list of the values that exist. Then if the length of that list isn’t 2, then you don’t have a correct input

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

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

                                            If you import math why do you still have to say math.sqrt and math.cos?
                                            But then other things like pow don’t have to say math.pow.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors