Python Forum
How to make general functions for ecuations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make general functions for ecuations
#7
Thanks, deanhysad.

Here a further improvement with your suggestion and other bits:

def pythagoras(h=None, x=None, y=None):
    '''Pythagorean Theorem:
    The area of the square whose side is the hypotenuse is equal
    to the sum of the areas of the squares on the other two sides.'''
    
    try:
        if h is None: return sqrt(x ** 2 + y ** 2)
        elif x is None: return sqrt(h ** 2 - y ** 2)
        elif y is None: return sqrt(h ** 2 - x ** 2)
        else: return "No unknown values to calculate." 
    except:
        return "Insufficient data."    

print(pitagoras(x=3, y=4))    # 5.0
print(pitagoras(h=5, x=3))    # 4.0
print(pitagoras(h=5, x=4))    # 3.0
print(pitagoras(h=5, x=4, y=4))    # No unknown values to calculate.
print(pitagoras(h=5))    # Insufficient data.
print(pitagoras(x=4))    # Insufficient data.
print(pitagoras(y=4))    # Insufficient data.
Reply


Messages In This Thread
RE: How to make general functions for ecuations - by Mondata - Mar-11-2021, 09:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,520 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  How do you make functions that take a variable that is not defined? magic 6 4,495 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Functions to make an image zoom in until a response is made sawilliams 1 2,156 Aug-02-2018, 08:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020