omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular
    1. Home
    2. OkieWolf

    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.


    • Profile
    • Following 0
    • Followers 0
    • Topics 3
    • Posts 30
    • Best 2
    • Controversial 0
    • Groups 0

    OkieWolf

    @OkieWolf

    2
    Reputation
    413
    Profile views
    30
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    OkieWolf Unfollow Follow

    Best posts made by OkieWolf

    • RE: If statement syntax error

      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' ```
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: Math

      I realize there is math.radians and math.degrees that does the same as radians = (degrees * 180)/pi and degrees = (radians * pi)/180.
      I also realize that some people say that radians are required for certain calculations. But I’ve worked in engineering for almost 30 years and have done thousands upon thousands of calculations and have never, not even once, had the need to calculate anything using radians. So why are radians so prevalent in coding especially?

      posted in Pythonista
      OkieWolf
      OkieWolf

    Latest posts made by OkieWolf

    • RE: If statement syntax error

      @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) 
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @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)  
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @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

      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @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) 
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @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)
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @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() 
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @ccc
      Done

      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      Here’s what I came up with. It’s much shorter and easier to understand and I GREATLY appreciate all the help and patience.
      This version does all the other one did plus more. This version will calculate the other side and angle based on two sides input and can now calculate any side of the triangle based on angle and any one of the three sides.

      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)
      	angle = float(v['angle'].text or 0)
      	if len([x for x in (hypotenuse, short, long, angle) if x]) != 2:
      		v['mess'].text = "Enter two fields"
      		v['mess'].background_color = 'red'
      		v['mess'].text_color = 'white'
      		return
      	if hypotenuse and not angle:
      		if short and not long:
      			v['long'].text = str(math.sqrt(hypotenuse ** 2 - short ** 2))
      			v['long'].text_color = 'green'
      			v['angle'].text = str(math.degrees(math.asin(short / hypotenuse)))
      			v['angle'].text_color = 'green'
      		elif long and not short:
      			v['short'].text = str(math.sqrt(hypotenuse ** 2 - long ** 2))
      			v['short'].text_color = 'green'
      			v['angle'].text = str(math.degrees(math.acos(long / hypotenuse)))
      			v['angle'].text_color = 'green'
      	else:
      		if not angle:
      			v['hypotenuse'].text = str(math.sqrt(short ** 2 + long ** 2))
      			v['hypotenuse'].text_color = 'green'
      			v['angle'].text = str(math.degrees(math.atan(short / long)))
      			v['angle'].text_color = 'green'
      		
      	if angle:
      		angle = math.radians(angle)
      		if short and not long and not hypotenuse:
      			v['hypotenuse'].text = str(short / (math.sin(angle)))
      			v['hypotenuse'].text_color = 'green'
      			v['long'].text = str(short / (math.tan(angle)))
      			v['long'].text_color = 'green'
      		elif long and not short and not hypotenuse:
      			v['hypotenuse'].text = str(long / (math.cos(angle)))
      			v['hypotenuse'].text_color = 'green'
      			v['short'].text = str(long * (math.tan(angle)))
      			v['short'].text_color = 'green'
      		elif angle and hypotenuse and not long and not short:
      			v['short'].text = str(hypotenuse * (math.sin(angle)))
      			v['short'].text_color = 'green'
      			v['long'].text = str(hypotenuse * (math.cos(angle)))
      			v['long'].text_color = 'green' 
      
      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      Agreed. It works fine if short and long are the same number, which equals 45 degrees.

      posted in Pythonista
      OkieWolf
      OkieWolf
    • RE: If statement syntax error

      @ccc

      I’m attempting to rewrite my code to be more efficient and am utilizing a couple of code snippets that a couple of you have supplied. However, in the below code I noticed that the output is 0.0 if both inputs happen to be the same number. I’m not understanding why.

      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['mess'].text = "Enter two fields"
      		v['mess'].background_color = 'red'
      		v['mess'].text_color = 'white'
      		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'].text = 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' ```
      posted in Pythonista
      OkieWolf
      OkieWolf