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:
Step 11 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 function. This result is then converted to an integer type (remember that the function returns a string type) using the 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 . This is, of course, entirely optional.
Notice that, in the statement I include the symbol 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).
Recommended textbook solutions
Write definitions for the following two functions: sumN(n) returns the sum of the first n natural numbers. sumNCubes (n) returns the sum of the cubes of the first n natural numbers. Then use these functions in a program that prompts a user for n and prints out the sum of the first n natural numbers and the sum of the cubes of the first n natural numbers.
An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for "random access memory." Write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. Note: the acronym should be all uppercase, even if the words in the phrase are not capitalized.