Using the following atomic weights carbon 12.0110 hydrogen 1.0079 oxygen 15.9994 revise your program for 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 C2H50H for ethyl alcohol, COOHC6H40COCH3 or C9H804 for aspirin, CH3CH2CH2CH2CH2CH2CH2CH3 or C8H18 for octane, and so forth. You should write an algorithm to analyse an input string of this form, using string indexing as in section 3.1 to look at individual characters in the string. You should accumulate the total counts of hydrogen, carbon, and oxygen in the compound, and then use the formula in your previous section 2.12 assignment to compute the molecular weight. I suggest you use a list of three entries 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 above and in section 2.12 have four decimal places). Below is my output for the four chemical formulas above. The user input will not appear in the output used by the zyBooks comparison tests when grading. There are also some hidden test cases. Enter a chemical formula, or just the enter key to quit: C2H5OH The molecular weight is 46.0688 Enter a chemical formula, or just the enter key to quit: COOHC6H4OCOCH3 The molecular weight is 180.1598 Enter a chemical formula, or just the enter key to quit: C9H804 The molecular weight is 180.1598 Enter a chemical formula, or just the enter key to quit: C8H18 The molecular weight is 114.2302 Enter a chemical formula, or enter key to quit:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Using Python, while loops, no for loops. Thank you

Using the following atomic weights
carbon
12.0110
hydrogen
1.0079
oxygen
15.9994
revise your program for 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 C2H50H for ethyl alcohol, COOHC6H4OCOCH3 or C9H804 for aspirin, CH3CH2CH2CH2CH2CH2CH2CH3 or
C8H18 for octane, and so forth. You should write an algorithm to analyse an input string of this form, using string indexing as in section 3.1
to look at individual characters in the string. You should accumulate the total counts of hydrogen, carbon, and oxygen in the compound, and
then use the formula in your previous section 2.12 assignment to compute the molecular weight. I suggest you use a list of three entries 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 above and in section 2.12
have four decimal places).
II
Below is my output for the four chemical formulas above. The user input will not appear in the output used by the zyBooks comparison
tests when grading. There are also some hidden test cases.
Enter a chemical formula, or just the enter key to quit: C2H5OH
The molecular weig
is 46.0688
Enter a chemical formula, or just the enter key to quit: COOHC6H4OCOCH3
The molecular weight is 180.1598
Enter a chemical formula, or just the enter key to quit: C9H8O4
The molecular weight is 180.1598
Enter a chemical formula, or just the enter key to quit: C8H18
The molecular weight is 114.2302
Enter a chemical formula, or enter key to quit:
Transcribed Image Text:Using the following atomic weights carbon 12.0110 hydrogen 1.0079 oxygen 15.9994 revise your program for 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 C2H50H for ethyl alcohol, COOHC6H4OCOCH3 or C9H804 for aspirin, CH3CH2CH2CH2CH2CH2CH2CH3 or C8H18 for octane, and so forth. You should write an algorithm to analyse an input string of this form, using string indexing as in section 3.1 to look at individual characters in the string. You should accumulate the total counts of hydrogen, carbon, and oxygen in the compound, and then use the formula in your previous section 2.12 assignment to compute the molecular weight. I suggest you use a list of three entries 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 above and in section 2.12 have four decimal places). II Below is my output for the four chemical formulas above. The user input will not appear in the output used by the zyBooks comparison tests when grading. There are also some hidden test cases. Enter a chemical formula, or just the enter key to quit: C2H5OH The molecular weig is 46.0688 Enter a chemical formula, or just the enter key to quit: COOHC6H4OCOCH3 The molecular weight is 180.1598 Enter a chemical formula, or just the enter key to quit: C9H8O4 The molecular weight is 180.1598 Enter a chemical formula, or just the enter key to quit: C8H18 The molecular weight is 114.2302 Enter a chemical formula, or enter key to quit:
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. So a possible session might look like:
Enter a chemical formula, or just the enter key to quit: 14H
Badly formed molecular formula; try again.
Enter a chemical formula, or just the enter key to quit: h14
Badly formed molecular formula; try again.
Enter a chemical formula, or just the enter key to quit: H14
The molecular weight is 14.1106
Enter a chemical formula, or the enter key to quit:
Transcribed Image Text: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. So a possible session might look like: Enter a chemical formula, or just the enter key to quit: 14H Badly formed molecular formula; try again. Enter a chemical formula, or just the enter key to quit: h14 Badly formed molecular formula; try again. Enter a chemical formula, or just the enter key to quit: H14 The molecular weight is 14.1106 Enter a chemical formula, or the enter key to quit:
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY