Hello,
I am completely stuck on a question I was assigned and was hoping for a little guidance to get me started.

The question is:
Create a program that outputs the molar mass of a molecule. The user will enter a molecule with each atom individually (CO2 would be entered as): C O O
(We are given a file to use) Parse the comma separated text file "elements.csv". The user should continue to be asked for more molecules until they enter a blank line.

I know I have to use dictionaries, keys and values to look them up in the file but I am lost as to how to start this code. Any help would be appreciated.

Thank you

Recommended Answers

All 2 Replies

Generally you begin with the input

The user will enter a molecule with each atom individually (CO2 would be entered as): C O O...The user should continue to be asked for more molecules until they enter a blank line.

Next would be the dictionary you spoke of, sign-->mass, and looking up each atom so as to add the mass to a total.

This is what I've done so far

def main():
    d = {}
    w = raw_input("Atom: ")
    f = open('elements.py.txt', 'r')
    for line in f:
        for word in line.split():
            if word == w:
                d.append(word)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.