Question
Write a program in PYTHON that uses the molecular weight of a chemical compound of hydrogen, carbon, and oxygen, so that it can take as
Write a program in PYTHON that uses the molecular weight of a chemical compound of hydrogen, carbon, and oxygen, so that it can take as user input a chemical formula of the form C2H5OH for ethyl alcohol, COOHC6H4OCOCH3 or C9H8O4 for aspirin, CH3CH2CH2CH2CH2CH2CH2CH3 or C8H18 for octane, and so forth. You could write a function to parse an input string of this form, which could return the total counts of hydrogen, carbon, and oxygen in the compound, and then use the formula in your previous section 2.11 assignment to compute the molecular weight. I suggest you use list for these three counts, and increment the appropriate count as you process the input string. Be careful to deal with the case when there is no number following the letter, which means the implied number is just one, and the case, as for octane above, when the number after a letter has more than one digit.
The program should keep outputting the prompt "Enter a chemical formula, or just the enter key to quit: " for the chemical formula of a new compound, until the user presses only the enter key without a chemical formula, in which case the program should terminate. The molecular weights should be printed with exactly four digits after the decimal point (since the atomic weights have four decimal places).
Modify your program so that if the user enters an invalid formula, for example, one that contains other characters besides digits and C, H, and O, or begins with a digit, the program will print the line "Badly formed molecular formula; try again." and then the input prompt again
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started