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
    11398
    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 @ccc last edited by

      @ccc
      Done

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

        OK. Now it is time to separate the business logic from the UI representation. We would need to put all the math logic in a function of the form:

        def triangle(a, b, hypotenuse=0, angle=45):
            pass  # math logic goes here
        

        Where triangle(3, 4) returns (3, 4, 5.0, 45)
        Where triangle(3, 0) would raise ValueError()

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

          It seems to me you could also dispense with most of the long / short specific logic, and just compute the other edge, then figure out which one is the long vs short after the fact.

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

            @ccc

            Please explain

            def triangle(a, b, hypotenuse=0, angle=45):
                pass  # math logic goes here
            Where triangle(3, 4) returns (3, 4, 5.0, 45)
            Where triangle(3, 0) would raise ValueError() 
            
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              This was an invitation for you or someone else to write a function that can take between 2 and 4 parameters and calculate all other variables or raise an exception if there is no possible solution. This function would allow us to isolate all UI operations from all math operations. It would make it easier to test our math operations without the UI and would allow us to easily port the math operations to a different platform.

              OkieWolf 2 Replies Last reply Reply Quote 0
              • OkieWolf
                OkieWolf @ccc last edited by OkieWolf

                @ccc

                Below will calculate short and long with given hypotenuse and angle but you have to enter zero for short and long input.

                import math
                
                def triangle(short, long, hypotenuse, angle):
                    pass  
                    if hypotenuse and angle:
                    	angle = math.radians(angle)
                    	print ('Short = ' + str(hypotenuse * (math.sin(angle))))
                    	print ('Long = ' + str(hypotenuse * (math.cos(angle))))
                    	
                triangle(0,0,10,30)
                
                1 Reply Last reply Reply Quote 0
                • OkieWolf
                  OkieWolf @ccc last edited by

                  @ccc

                  This will calculate any side or angle. But input still has to be zero for unknowns at input.

                  import math
                  
                  def triangle(short, long, hypotenuse, angle):
                      pass
                      if len([x for x in (hypotenuse, short, long, angle) if x]) == 2:
                      	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))))
                      else:
                      	print ("ERROR")
                  
                  triangle(5,0,10,0) 
                  
                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by ccc

                    Step 0: drop the pass line.
                    Step 1:

                        if len([x for x in (hypotenuse, short, long, angle) if x]) != 2:
                            raise ValueError("Exactly two parameters must be zero.")
                    

                    Step 2: change triangle() so it never prints but instead returns the answer with the statement return short, long, hypotenuse, angle
                    Step 3: change triangle(5,0,10,0) to print(triangle(5,0,10,0)).

                    Step 1: is called a fast-fail which is safer and more readable than failing later in the function. Basically is says "I see a bad situation (bad input data) so I am going to raise an exception and stop processing".

                    mikael OkieWolf 2 Replies Last reply Reply Quote 0
                    • mikael
                      mikael @ccc last edited by

                      @ccc, I am sure I just saw you using count somewhere:

                      if (hyp, ...).count(0) != 2:
                          raise ...
                      
                      1 Reply Last reply Reply Quote 0
                      • OkieWolf
                        OkieWolf @ccc last edited by OkieWolf

                        @ccc

                        I don’t understand step 2

                        Step 2: change triangle() so it never prints but instead returns the answer with the statement return short, long, hypotenuse, angle.

                        Are you wanting me to change....
                        def triangle(short, long, hypotenuse, angle):

                        Also, changing to print(triangle(5,0,10,0)) adds “none” after returned values

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

                          def triangle(short, long, hypotenuse, angle):
                              # Your code goes here...
                              # Your code contains no print() calls.
                              return short, long, hypotenuse, angle
                          
                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp last edited by cvp

                            Your code contains no print() calls, like

                                    if hypotenuse and angle:
                                        angle = math.radians(angle)
                                        short = hypotenuse * math.sin(angle)
                                        long  = hypotenuse * math.cos(angle) 
                            
                            OkieWolf 1 Reply Last reply Reply Quote 0
                            • OkieWolf
                              OkieWolf @cvp last edited by

                              @cvp

                              I still don’t know what you’re wanting. My code has many print calls.
                              Adding return short, long, hypotenuse, angle where you have it above won’t do anything because it’s part of the last elif. If I add it in line with def Tringle it errors.

                              import math
                              
                              def triangle(short, long, hypotenuse, angle):
                                  pass
                                  if len([x for x in (hypotenuse, short, long, angle) if x]) == 2:
                                      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))))
                                  else:
                                      print ("ERROR")
                              
                              triangle(5,0,10,0)  
                              
                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @OkieWolf last edited by

                                @OkieWolf I understand, I only want to show what @ccc adviced. The triangle code without any print and only one print of triangle results

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

                                  @cvp

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

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

                                      This post is deleted!
                                      cvp 2 Replies Last reply Reply Quote 0
                                      • cvp
                                        cvp @ccc last edited by

                                        @ccc as short and long are différent, why do you think angle could be 45°?

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

                                          @ccc error, you use angle instead of radians_angle

                                              else:  # hypotenuse and angle
                                                  short = hypotenuse * sin(radians_angle)
                                                  long = hypotenuse * cos(radians_angle) 
                                          
                                          1 Reply Last reply Reply Quote 0
                                          • ccc
                                            ccc last edited by

                                            Thanks! Now you know why I failed trig back in the day...

                                            Both issues fixed in the code above.

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