song.py: 3 points
savings.py: 3 points
In this portion of the lab you’ll be building several short programs that each utilize a single for loop.
You might be familiar with the sea shanty ‘Ninety-Nine Bottles of Beer on the Wall’, whose first two verses are:
99 bottles of beer on the wall
99 bottles of beer!
Take one down, pass it around
98 bottles of beer on the wall!
98 bottles of beer on the wall
98 bottles of beer!
Take one down, pass it around
97 bottles of beer on the wall!
and eventually ends with the
0 bottles of beer on the wall!
Different regions have different variants of this song, changing the item on the wall (e.g., pop, soda, root beer, water).
Your goal with the
song.py program is to turn this song into a Mad Libs®-like game, where the user inputs (1) what the item is, (2) where the item is, (3) what happens to the item in each verse, and (3) how many verses of the song the computer should “sing.”
Each verse of the song should be one iteration of a loop. To know how to setup that loop, think about the three verses of the song above: what changes between the first and the second verse? Between the second and the third? What does the last verse look like? How might a loop and its loop variable be used here?
Also, think about the user’s inputs and where they go in the verses above.
As a friendly reminder, each
print() statement prints out a single line. So to get four lines in a verse, the simplest approach is to use four
Finally, do not worry about grammar. Notice that the output above includes “1 white squirrels” which is grammatically incorrect, but it is okay for our assignment.
One run of the program might be:
What item would you like? white squirrel Where is it? in Tappan Square What happens to it? watch one scurry away How many verses would you like? 3 Your song is: 3 white squirrels in Tappan Square 3 white squirrels! watch one scurry away 2 white squirrels in Tappan Square 2 white squirrels in Tappan Square 2 white squirrels! watch one scurry away 1 white squirrels in Tappan Square 1 white squirrels in Tappan Square 1 white squirrels! watch one scurry away 0 white squirrels in Tappan Square
Note that there is an
s at the end of
"white squirrel" on multiple lines, and that there is a
! on line two.
Next, we will extend our
simple.py program from Lab 1 to make it more useful for financial planning. Instead of only depositing money at the start of the semester, you recently started a job on campus and can now make regular deposits at the end of each month. You’d like to know how much you’ll have in your savings account after a given number of months (e.g., at the end of the school year before summer). We will create a program called
savings.py that performs these calculations for us.
For example, suppose your initial savings is $100, the monthly interest rate is 2%, and you plan on contributing $20 a month for 3 months.
Because we have a new account balance after each month, you will need to create a variable that keeps track of the current balance in the user’s savings account. That variable will start by holding the user’s initial deposit, then will be be updated and printed each month (i.e., each iteration of your
As pointed out above, you’ll need to truncate your current balance to 2 decimal places. One clever way to do this: after you have updated the balance by adding in this month’s interest and regular contribution, you should multiply your value by 100 (now your number represents cents), convert the value to an
int (now you’ve tossed out any fraction of a cent that might have been there), and then divide by 100 (to get back to dollars again).
Based on the example above, a run of the
savings.py program should appear as follows.
Welcome to the Interest Calculator! Enter your initial savings: 100 Enter the monthly interest rate: 0.02 Enter your monthly contribution: 20 How many months would you like computed: 3 Initially you put in $100 After month 1 you would have $122.0 After month 2 you would have $144.44 After month 3 you would have $167.32