# Molarity form calculation using Python script

I have the below formula:

Molarity = Weight/(molar mass*volume)

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’)

if Mol == ‘’:
print( float(W)/(float(MM)float(Vol)) )
elif W == ‘’:
print( float(Mol)float(MM)float(Vol) )
elif MM==’’:
print( float(W)/(float(Mol)float(Vol)))
elif Vol ==’’:
print( float(W)/(float(Mol)*float(MM)) )

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:

Why not initialize your variables to the value of one, from the outset. Doing that will have a null effect on the equations.

Hi Rob, can you please elaborate a bit more on that?

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.

No worries.

Help me to understand this.

So…

Mass = Concentration x Volume x Molecular Weight

Concentration = mol / Volume

Molecular Weight = Mass / mol

Volume = Mass / (mol x Molecular Weight)

Molecular Mass = Molecular Weight / (mol x Volume)

mol = Molecular Weight / (Molecular Mass x Volume)

… have I got that right?

If we get the math correct, then this will almost code itself.

“Why not initialize your variables to the value of one, from the outset.”

If a value of 1 is a trigger to solve for that variable, how would you
ask to calculate the molarity of 1g of NaCl in 1 cm^3 of water?

With your scheme, three variables will be set to 1 and there is no
way for the user to indicate which one should be solved for.

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.

``````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: ')
``````

… then act upon the `find`

Hey.

Not sure how far along you are with this, or if you still need help, but this is what I have so far:

``````molecular_weight = float(input('Molecular Weight (g/mol): '))

find = input('What do you want to calculate?'+'\n'+'(m)ass, (v)olume or (c)oncentration: ')

if find == 'm':  # mass = concentration * volume * molecular_weight
concentration = float(input('Desired concentration (M): '))
volume = float(input('Desired volume (L): '))
mass = concentration * volume * molecular_weight
if mass > 1:
scale = 'g'
else:
mass *= 1000 # convert to milligrams
scale = 'mg'
print('Mass:',mass,scale)
``````

Hopefully it will show you the way forward. If you still need help, then feel free to ask.

Thanks Rob.

To simplify this lets consider the below equation:
A= B/(C*D)

My query is to calculate any 1, when I give the other 3, (without using 4 different formulas)

A, if B,C,D given
B, if A,C,D given

You’re welcome.

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.

You can try the sympy library.

https://docs.sympy.org/latest/modules/solvers/solvers.html

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.