Question

Write a program that determines the molecular weight of a hydrocarbon based on the number of hydrogen, carbon, and oxygen atoms. You should use the following weights:

 Atom  Weight  (grams / mole )H1.0079C12.011O15.9994\begin{array}{cc}{\text { Atom }} & {\text { Weight }} \\ \hline & {\text { (grams } / \text { mole })} \\ \hline \mathrm{H} & {1.0079} \\ {\mathrm{C}} & {12.011} \\ {\mathrm{O}} & {15.9994}\end{array}

Solution

Verified

Step 1

1 of 3

First we write down the constants that will be used in the calculations. Here the common convention is that constant variables (i.e. ones that shouldn't be changed at any point in the program) should be in all-caps.

Next we prompt the user to type in the number of each type of element using the input\texttt{input} function. This result is then converted to an integer type (remember that the input\texttt{input} function returns a string type) using the int\texttt{int} function.

Then we calculate the molecular weight.

And finally we display the weight to the user. Note that I decided to round the molecular weight in an attempt to combat the fact that Python tends to approximate these numbers and I didn't want to display an answer ending in something like 999997\texttt{999997}. This is, of course, entirely optional.

Notice that, in the print\texttt{print} statement I include the symbol \\texttt{\text{\textbackslash}} at the end of the line. This is just how you can tell Python that the statement continues on to the line. Then the next line must be indented, but the amount is up to you (though if you need to do this to multiple lines, they all need to be the same). I chose to indent to the point where I thought it looked best (most readable).

Create an account to view solutions

By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Continue with GoogleContinue with Facebook

Create an account to view solutions

By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Continue with GoogleContinue with Facebook

Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface 5th Edition by David A. Patterson, John L. Hennessy

Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionDavid A. Patterson, John L. Hennessy
220 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionRamez Elmasri, Shamkant B. Navathe
687 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionCharles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
726 solutions

Related questions