Function
Calculates values for user defined equations. Just a concept right now. I plan to add many more equations.
Equations in code
Density:
$$ p = \frac{m}{v} $$
Speed:
$$ s = u + a \cdot t $$
class Equations:
def __init__(self) -> None:
self.p = lambda m, v: m / v
self.m = lambda p, v: m * v
self.v = lambda p, m: m / p
self.s = lambda u, a, t: u + a * t
def calculate() -> None:
density = Equations()
answer = density.p(1.23, 1.66)
print(f'density: {answer:.2f}')
speed = Equations()
answer = speed.s(3, 4, 5)
print(f'speed: {answer:.2f}')
if __name__ == '__main__':
calculate()
Output:
density: 0.74
speed: 23.00
Notes
I am a beginner so I’m definitely doing things wrong here, suggestions welcome.
Is this OOP and classes done right?
Background
I asked a similar question like this and the answer I got was from a very kind user and was very elegant, however it works for 1 equation, and this users suggestion to make it work for multiple equations went over my head:
solve
to an abstract class, I looked up abstract classes, you importABC
and pass that in but I just don’t know how to implement it, I really wanted to use your code as it rearranges equations by itself and only allows real numbers, the only problem is I can’t find material which would enable me to do as you said, any resources or tips you can recommend? \$\endgroup\$