How to get molecular weight of a compound in python?

2024/5/20 17:47:50

User inputs a formula, for example: C12H2COOH

We have to calculate its molecular weight given that C = 12.01, H = 1.008 and O = 16. We were told to be careful of elements with double digits after it and elements with no numbers after it. The program also keeps asking for a chemical formula and exits when you press enter.

I've tried using dictionaries, for loops and while loops. I've gotten to calculate compounds with single digits after the elements like C2H2 but if I put double digits or put no numbers next to the element, it fails. I was also looking at how to separate strings without deleting delimiters as a possible route? How would you guys approach this problem? Any help would be appreciated, thank you!

Here is what I have so far. It's very messy.

xxx = ["H", "C", "O"]
elements = set(xxx)
while(True):chemical_formula = input("Enter chemical formula, or enter to quit: ")if chemical_formula == "":breakelse:characters = list(chemical_formula)n = 0print(characters)for i in characters:if characters[n] == "C":c = 12.0107if elements.intersection(set(characters[n+1])):print(c)else:number = int(characters[n+1])print(number*c)elif characters[n] == "H":h = 1.00794if elements.intersection(set(characters[n+1])):print(h)else:number = int(characters[n+1])print(number*h)elif characters[n] == "O":o = 15.9994if elements.intersection(set(characters[n+1])):print(c)else:number = int(characters[n+1])print(number*o) else:numero = int(i)print(i*0)n = n+1
Answer

First:

pip install molmass

Then:

from molmass import FormulaFormula('H2O').isotope.mass
>> 18.01056468403   #  monoisotopic massFormula('H2O').mass  
>> 18.015287        # molecular mass
https://en.xdnf.cn/q/73310.html

Related Q&A

How to install Numpy and Pandas for AWS Lambdas?

Problem: I wanted to use Numpy and Pandas in my AWS lambda function. I am working on Windows 10 with PyCharm. My function compiles and works fine on local machine, however, as soon as package it up and…

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesnt get rid of it. Opening a sec…

Using python modules in node.js

Is it possible to create a glue that makes it possible for python modules (more specifically, library bindings) to be used in node.js? Some of the data structures can be directly mapped to V8 objects …

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.What I am trying to do:I am …

How do I find my program name?

ProblemI am unable to write to a different log than the default one using syslog. I am unsure if maybe my app name is wrong in my configuration. Do "program name" and "process name"…

Is it possible to automate the execution of a Python script using Microsoft Flow?

I want to execute a snippet of python code based on some trigger using Microsoft-Flow. Is there a way to do this? Basically I am exploring on Powerapps and Microsoft-Flow. I have data in powerapp, I c…

Python: Selecting numbers with associated probabilities [duplicate]

This question already has answers here:Closed 13 years ago.Possible Duplicates:Random weighted choiceGenerate random numbers with a given (numerical) distribution I have a list of list which contains …

Weights and Biases: Login and network errors

I recently installed Weights and Biases (wandb) for recording the metrics of my machine learning projects. Everything worked fine when connected to wandb cloud instance or when I used a local docker im…

Python: Opening a file without creating a lock

Im trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I dont want my script to prevent that by locking the file; the file should be abl…

Tensorflow dataset questions about .shuffle, .batch and .repeat

I had a question about the use of batch, repeat and shuffle with tf.Dataset.It is not clear to me exactly how repeat and shuffle are used. I understand that .batch will dictate how many training exampl…