Mar-07-2021, 06:56 PM
Hi there.
I am quite new in Python, so please excuse me if this question is somewhat too simple.
I am trying to guess how to create a function that resolves a given ecuation independently of which parameters are introduced, so that:
- The function returns always all the values regardless of which parameters are introduced (provided they are enough for making the calculations).
- The function should inform if there are no unknown values (this is, just in case the user inadvertently plugs in all the values).
- The function should inform if there are no enough known values to operate (for instance, if calling the function there is no value for any parameter at all)
One example will clarify what I mean.
This is the solution I have found so far, but I am sure there has to be a better way to do it.
This example is for the ecuation "y = 2 * x + 3". It has only 2 variables, but the same idea should be applicable to any number of variables.
In this case, if we know at least 2 variables it is easy to calculate the others with Pythagorean Theorem, sin and cos.
The function should work as follows:
So, I guess my real question is if there is a way to simplify this process, any other form of programming this kind of functions.
Many thanks.
I am quite new in Python, so please excuse me if this question is somewhat too simple.
I am trying to guess how to create a function that resolves a given ecuation independently of which parameters are introduced, so that:
- The function returns always all the values regardless of which parameters are introduced (provided they are enough for making the calculations).
- The function should inform if there are no unknown values (this is, just in case the user inadvertently plugs in all the values).
- The function should inform if there are no enough known values to operate (for instance, if calling the function there is no value for any parameter at all)
One example will clarify what I mean.
This is the solution I have found so far, but I am sure there has to be a better way to do it.
This example is for the ecuation "y = 2 * x + 3". It has only 2 variables, but the same idea should be applicable to any number of variables.
def ecuation(x = None, y = None): '''Resolves ecuation "y = 2 * x + 3" given any one value.''' if x == None and y == None: return("Introduce at least one value.") elif x == None and y != None: y = float(y) x = (y - 3) / 2 elif x != None and y == None: x = float(x) y = 2 * x + 3 else: return("No unknown values.") return x, y print(ecuation()) # Introduce at least one value. print(ecuation(x=4)) # (4.0, 11.0) print(ecuation(y=13)) # (5.0, 13.0) print(ecuation(x=23, y=43)) # No unknown values.My goal is just to find an easy way of creating functions that represent all kind of ecuations and calculate whatever values are unknown given the known ones. For instance, to create a function that always resolves the Pythagorean Theorem plus the angle regardless of which (2) values are introduced:
pythagoras(h, x, y, ang)where h = hypotenuse, x = contiguos leg, y = opposite leg, ang = angle.
In this case, if we know at least 2 variables it is easy to calculate the others with Pythagorean Theorem, sin and cos.
The function should work as follows:
pythagoras(h=23, x=3) # (23, 3, and whatever values y and ang have) pythagoras(ang=90, y=34) # (value for h, value for x, 34, 90) pythagoras(h=23, x=3, y=45, ang=32) # No unknown values. pythagoras(ang=4) # Not enough parameters.Of course we could make an independent function for every case, but that would mean to have many different functions, one for every different case (6 (8-2) for 3 variables, 14 (16-2) for 4 variables, 30 (32-2) for 5 variables, etc.), so quite complicated.
So, I guess my real question is if there is a way to simplify this process, any other form of programming this kind of functions.
Many thanks.