Loopin'

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.

Sea Shanty

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.”

ReadMe

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 print() statements.

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.

Savings Calculator

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.

  • Then during the first month, you’d earn $2 of interest (i.e., $100 * 0.02). If we add that interest with your balance of $100 and your monthly contribution of $20, you would have a new balance of $122 after the first month.
  • After the second month, your interest would be $2.44 (i.e., $122 * 0.02), and adding another $20 monthly contribution means you would then have a new balance of $144.44.
  • Repeating this process one more time, after the third month, you’d have a balance of $167.32. Technically, the amount you’d have after month 3 would be $167.3288, but most accounts don’t keep track of fractions of pennies (as fans of the movie Office Space may know), so you’ll want to truncate your new balance to two decimal places before printing the amount to the user.

ReadMe

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 for loop).

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