Question

ASAP I need this code... Design and implement in Python language, a program that calculates the...

ASAP I need this code...

Design and implement in Python language, a program that calculates the molecular weight of a formula. Your program should accept a formula in whatever form you choose as input from the user, and should display the molecular weight for that formula. Your program must be able to handle formulas that:

  • consist of a single element, e.g., He    (worth 40 points)
  • consist of a sequence of elements, e.g., H H O    (worth 20 points)
  • contain numeric constants, e.g., H2O    (worth 20 points)
  • contain nested formulas, e.g., (NH4)2SO4    (worth 20 points)

The representation of data and the algorithm you choose for your implementation are entirely up to you. The interface for your program should clearly describe the expected input form so the user can interact effectively. Grading emphasis for this assignment is on correctness only -- inefficiency and inelegance may be commented on but will not adversely affect your grade except in extreme cases.

The periodic table of elements can be found in many sources, incuding numerous Web pages (e.g., www.chemicalelements.com/show/mass.html). A list of elements with their atomic weights is written as raw text below:

    H    1.0080    He   4.0026    Li   6.9410    Be   9.0122    B   10.8100

    C   12.0110    N   14.0067    O   15.9994    F   18.9984    Ne  20.1790

    Na  22.9898    Mg  24.3050    Al  26.9815    Si  28.0860    P   30.9738

    S   32.0600    Cl  35.4530    Ar  39.9480    K   39.1020    Ca  40.0800

    Sc  44.9559    Ti  47.9000    V   50.9414    Cr  51.9960    Mn  54.9380

    Fe  55.8470    Co  58.9332    Ni  58.7100    Cu  63.5460    Zn  65.3700

    Ga  69.7200    Ge  72.5900    As  74.9216    Se  78.9600    Br  79.9040

    Kr  83.8000    Rb  85.4678    Sr  87.6200    Y   88.9059    Zr  91.2200

    Nb  92.9064    Mo  95.9400    Tc  98.9062    Ru 101.0700    Rh 102.9055

    Pd 106.4000    Ag 107.8680    Cd 112.4000    In 114.8200    Sn 118.6900

    Sb 121.7500    Te 127.6000    I  126.9045    Xe 131.3000    Cs 132.9055

    Ba 137.3400    La 138.9055    Ce 140.1200    Pr 140.9077    Nd 144.2400

    Pm 145.0000    Sm 150.4000    Eu 151.9600    Gd 157.2500    Tb 158.9254

    Dy 162.5000    Ho 164.9303    Er 167.2600    Tm 168.9342    Yb 173.0400

    Lu 174.9700    Hf 178.4900    Ta 180.9479    W  183.8500    Re 186.2000

    Os 190.2000    Ir 192.2200    Pt 195.0900    Au 196.9665    Hg 200.5900

    Tl 204.3700    Pb 207.2000    Bi 208.9806    Po 210.0000    At 210.0000

    Rn 222.0000    Fr 223.0000    Ra 226.0254    Ac 227.0000    Th 232.0381

    Pa 231.0359    U  238.02900   Np 237.0482    Pu 242.0000    Am 243.0000

    Cm 247.0000    Bk 249.0000    Cf 251.0000    Es 254.0000    Fm 253.0000

    Md 256.0000    No 256.0000    Lr 257.00

Homework Answers

Answer #1

chemical_formula = input("Enter chemical formula: ")
atomicMass = {"He": 4.0026, "H" : 1.0080, "O" : 15.9994, "C" : 12.0110, "N" : 14.0067, "S" : 32.0600}

molecule = "He"
mass = atomicMass["He"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

molecule= "H2O"
mass = atomicMass["H"] * 2 + atomicMass["O"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

molecule= "H"
mass = atomicMass["H"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

molecule= "H"
mass = atomicMass["H"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

molecule= "O"
mass = atomicMass["O"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

molecule = "(NH4)2SO4"
mass = atomicMass["NH*4"] * 2 + atomicMass["SO*4"]
print("\nMolecule: " + molecule + " - Mass: " + str(mass))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute...
Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then call sqrt(). For Java, you’ll...
I have provided the information for the lab below. I want to see the chemical reactions...
I have provided the information for the lab below. I want to see the chemical reactions that take place during each step of the experiment. So in the procedures when a chemical was added i would like to see what the products and byproducts that were formed. I also want to see one over all reaction for this experiment to just get an overview of what exactly is happening during the experiment. Thank you:) Part B: Synthesis of [Co(NH3)5Cl]Cl2 Objective:...