This homework will require you to write some basic programs in python. You should save your programs in your cs21/homework/2 directory. A skeleton version of each program you will write will appear in your cs21/homework/2 if you run update21. The program handin21 will only submit files in this directory.
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page
Some of the problems will have optional components that allow you to
further practice your skills in python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
[From Zelle, Chapter 3, Problem 3] Write a program by editing the file atomic.py that determines the total molecular weight of a molecule based on the number of hydrogen, carbon, and oxygen atoms it contains. You should use the following weights.
Atom | Weight (g/mol) |
---|---|
H | 1.0079 |
C | 12.011 |
O | 15.9994 |
You program should prompt the user for the number of atoms of each type and display the total weight with the appropriate units. Your output should be formatted to four decimal places. Save your file as atomic.py in your cs21/homework/2 directory.
A sample run is shown below:
Enter number of Hydrogen atoms: 3 Enter number of Carbon atoms: 2 Enter number of Oxygen atoms: 1 Weight of molecule is 43.0451 g/mol
[From Zelle, Chapter 2, Problem 6] Write a program by editing the file invest.py that computes the value of an interest bearing investment after n years. You should prompt the user for:
Your program should print out a table that contains the following
You may want to look at the futval.py program in section 2.7 of the textbook. You can get a copy of this program by using update21
A sample run is shown below
Enter amount invested at start of each year: 100 Enter the annual interest rate in percent: 10 Enter number of years you plan to invest: 10 Year Start Interest End Total Value (year) Value Interest -------------------------------------------------- 1 100.00 10.00 110.00 10.00 2 210.00 21.00 231.00 31.00 3 331.00 33.10 364.10 64.10 4 464.10 46.41 510.51 110.51 5 610.51 61.05 671.56 171.56 6 771.56 77.16 848.72 248.72 7 948.72 94.87 1043.59 343.59 8 1143.59 114.36 1257.95 457.95 9 1357.95 135.79 1493.74 593.74 10 1593.74 159.37 1753.12 753.12Optional extensions
[From Zelle, Chapter 3, Problem 16] A Fibonacci sequence is a
sequence of numbers where each successive number is the sum of the
previous two. The classic Fibonacci sequence begins with
1, 1, 2, 3, 5, 8, 13, ...
Write a program by editing fibo.py that computes
the nth Fibonacci number where
n is input by the user. For example for n=6 the output
should be 8.
A few sample runs are shown below.
Which Fibonacci number would you like?: 1 Fib 1 = 1 Which Fibonacci number would you like?: 2 Fib 2 = 1 Which Fibonacci number would you like?: 7 Fib 7 = 13 Which Fibonacci number would you like?: 8 Fib 8 = 21 Which Fibonacci number would you like?: 9 Fib 9 = 34Optional extensions
An example is shown below for interest compounded semi-annually:
Enter amount invested at start of each year: 100 Enter the annual interest rate in percent: 10 Enter number of years you plan to invest: 5 Enter the number of times interest is computed each year: 2 Year Start Interest End Total Value (term) Value Interest -------------------------------------------------- 1.00 100.00 5.00 105.00 5.00 1.50 105.00 5.25 110.25 10.25 2.00 210.25 10.51 220.76 20.76 2.50 220.76 11.04 231.80 31.80 3.00 331.80 16.59 348.39 48.39 3.50 348.39 17.42 365.81 65.81 4.00 465.81 23.29 489.10 89.10 4.50 489.10 24.46 513.56 113.56 5.00 613.56 30.68 644.23 144.23 5.50 644.23 32.21 676.45 176.45Compare this to the other table above. What happens as you compound the interest more frequently (quarterly, monthly, daily, hourly). For these tests, you may just want to print out one line for each year, even though you are computing interest many times.