If any 3 inputs are given, the program gives the unknown from the equation. The below code is a simple approach to achieve this:
print(‘Enter the Following values. For unknown values type nothing’)
Mol = input(‘Enter Molarity \n’)
W = input(‘Enter Weight \n’)
MM = input(‘Enter Molar Mass \n’)
Vol = input(‘Enter Volume \n’)
What I am looking for is - if I give Mol,MM,Vol, the program knows that I didnt give any value for W and that it calculates the same.The same applies to other variables. Any inputs will be appreciated. Please check the below link for better understanding: https://www.graphpad.com/quickcalcs/molarityform/
Without looking at this in any detail, I’m a little unsure, but I think this is kind of what you’re after:
print('Enter the Following values. For unknown values type 1.0')
Mol = float(input('Enter Molarity \n'))
W = float(input('Enter Weight \n'))
MM = float(input('Enter Molar Mass \n'))
Vol = float(input('Enter Volume \n'))
if Mol == 1:
print(W/(MM*Vol))
elif W == 1:
print(Mol*MM*Vol)
elif MM == 1:
print(W/(Mol*Vol))
elif Vol == 1:
print(W/(Mol*MM))
Edit: code change.
I know nothing about chemistry, so if this is not right, forgive me.
Least ways, it should give you a better hook on the code.
But if we dont enter 1, is there a way to retrieve the value of any variable out of the 4 variables. (assuming the value is not 1)
Basically the requirement is to retrieve output for any missing variable out of all these 4 variables when the other 3 inputs are provided. Sounds a bit complex, but I appreciate any inputs.
Yeah. I was trying to adapt the OPs code, rather than re-code it the way that I would code it.
At this point I’m not too sure if he’s trying to learn how to solve the math, or if he’s trying to learn how to code the math using Python. Or maybe he’s trying to do both, which is why I wanted the OP to get the math down first; as I said, the code will follow.
I don’t think that what I have down for the math is correct.
I would start with something like…
molecular_weight = float(input('Please enter a value for the Molecular Weight: '))
find = input('What do you want to calculate?'+'\n'+'(m)ass, (v)olume or (c)oncentration: ')
Maybe you have your reasons for not wanting to use four different formulas, but I would simply use the math, as it’s much easier to do things that way:
A = B/(C*D)
B = A*C*D
C = B/(A*D)
D = B/(A*C)
Maybe it’s possible within one formula, but that’s beyond my skills; sorry.
sympy is a computer algebra systom (CAS) capable of symbolic mathematics. It includes over 350 modules and tens or hundreds of thousands of lines of code, plus many more thousands of lines of documentation and hundreds of tests. It is over 70 MB of code.
Or you could write four tiny functions, one each to solve each variable, plus another tiny bit of code to decide which function to call.