CS21 Lab4: while loops

Due Saturday (February 21) before midnight

This lab assignment requires you to write three programs in Python. First, run update21. This will create the cs21/labs/04 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/04 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/04
$ pwd
/home/your_user_name/cs21/labs/04
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Programming tips

As you write your first programs, start using good programming practices now:


1. Fake Download Status Bar
download

Write a program that displays a download status bar to the terminal screen. Your program should ask for the size of the file (in MB), and then display the status of the download as shown below. Since we can't really download a file, we will simulate this with a while loop and the random library. Assume the file is downloaded in chunks, where the size of each chunk is chosen using randrange().

Each time another chunk of the file is downloaded, your status bar should update the display. The program should continue downloading until 100% of the file is done.

Here are some sample runs of the program.

$ python download.py
size of download in MB: 400
0%                                                    100% (  0MB)
0% #                                                  100% (  8MB)
0% ###                                                100% ( 27MB)
0% ######                                             100% ( 51MB)
0% #######                                            100% ( 59MB)
0% #########                                          100% ( 75MB)
0% #############                                      100% (108MB)
0% #############                                      100% (109MB)
0% ################                                   100% (135MB)
0% ##################                                 100% (144MB)
0% ###################                                100% (154MB)
0% ###################                                100% (155MB)
0% #######################                            100% (190MB)
0% #########################                          100% (206MB)
0% ##########################                         100% (213MB)
0% ##############################                     100% (244MB)
0% ##################################                 100% (278MB)
0% #####################################              100% (298MB)
0% #######################################            100% (313MB)
0% #########################################          100% (328MB)
0% ###########################################        100% (351MB)
0% ################################################   100% (384MB)
0% ################################################## 100% (400MB)

$ python download.py
size of download in MB: 130
0%                                                    100% (  0MB)
0% #####                                              100% ( 14MB)
0% ###############                                    100% ( 39MB)
0% #################                                  100% ( 46MB)
0% #####################                              100% ( 56MB)
0% ####################################               100% ( 95MB)
0% ################################################   100% (126MB)
0% #################################################  100% (129MB)
0% ################################################## 100% (130MB)

Requirements:



Credit Card Payments
credit cards

Write a program that shows how credit card debt payments are affected by the amount paid each month. For example, your program could show the difference between paying only the minimum amount each month versus paying an additional $100.

Assume the following about the credit card and the debt:

Your program should ask the user how much extra they want to pay each month, above the minimum amount (e.g., 10 dollars, 20, etc). It should then calculate the interest and payments each month, until the debt is paid.

Here's how the program should work:

$  python ccpayoff.py
Credit Card Balance: $2000.00
      Interest Rate: 20%
    Minimum Payment: 3%
------------------------------
Extra amount paid each month:  0 
------------------------------
Balance  InterestAmt   PrinAmt    Payment     Paid   Months
$2000.00   $  33.33   $  26.67   $  60.00   $  60.00     1
$1973.33   $  32.89   $  26.31   $  59.20   $ 119.20     2
$1947.02   $  32.45   $  25.96   $  58.41   $ 177.61     3
$1921.06   $  32.02   $  25.61   $  57.63   $ 235.24     4
$1895.45   $  31.59   $  25.27   $  56.86   $ 292.11     5
$1870.18   $  31.17   $  24.94   $  56.11   $ 348.21     6
$1845.24   $  30.75   $  24.60   $  55.36   $ 403.57     7
$1820.64   $  30.34   $  24.28   $  54.62   $ 458.19     8
$1796.36   $  29.94   $  23.95   $  53.89   $ 512.08     9
...
...
$  70.31   $   1.17   $   8.83   $  10.00   $4175.18   176
$  61.48   $   1.02   $   8.98   $  10.00   $4185.18   177
$  52.51   $   0.88   $   9.12   $  10.00   $4195.18   178
$  43.38   $   0.72   $   9.28   $  10.00   $4205.18   179
$  34.11   $   0.57   $   9.43   $  10.00   $4215.18   180
$  24.67   $   0.41   $   9.59   $  10.00   $4225.18   181
$  15.09   $   0.25   $   9.75   $  10.00   $4235.18   182
$   5.34   $   0.09   $   5.34   $   5.43   $4240.60   183
$  python ccpayoff.py
Credit Card Balance: $2000.00
      Interest Rate: 20%
    Minimum Payment: 3%
------------------------------
Extra amount paid each month:  100 
------------------------------
Balance  InterestAmt   PrinAmt    Payment     Paid   Months
$2000.00   $  33.33   $ 126.67   $ 160.00   $ 160.00     1
$1873.33   $  31.22   $ 124.98   $ 156.20   $ 316.20     2
$1748.36   $  29.14   $ 123.31   $ 152.45   $ 468.65     3
$1625.04   $  27.08   $ 121.67   $ 148.75   $ 617.40     4
$1503.38   $  25.06   $ 120.05   $ 145.10   $ 762.50     5
$1383.33   $  23.06   $ 118.44   $ 141.50   $ 904.00     6
$1264.89   $  21.08   $ 116.87   $ 137.95   $1041.95     7
$1148.02   $  19.13   $ 115.31   $ 134.44   $1176.39     8
$1032.72   $  17.21   $ 113.77   $ 130.98   $1307.37     9
$ 918.95   $  15.32   $ 112.25   $ 127.57   $1434.94    10
$ 806.69   $  13.44   $ 110.76   $ 124.20   $1559.14    11
$ 695.94   $  11.60   $ 109.28   $ 120.88   $1680.02    12
$ 586.66   $   9.78   $ 107.82   $ 117.60   $1797.62    13
$ 478.84   $   7.98   $ 106.38   $ 114.37   $1911.98    14
$ 372.45   $   6.21   $ 104.97   $ 111.17   $2023.16    15
$ 267.49   $   4.46   $ 105.54   $ 110.00   $2133.16    16
$ 161.94   $   2.70   $ 107.30   $ 110.00   $2243.16    17
$  54.64   $   0.91   $  54.64   $  55.55   $2298.71    18

Here's the algorithm for calculating monthly interest and payments (using the first line from the last example above):


3. Guess N Coin Flips
coinflips Write a program to play "guess-N coin flips". Your program should ask for the number of coinflips to guess, then repeat the following until the user guesses the correct sequence of coinflips:

Below are some sample runs showing how your program should work.

$ python guessNflips.py
number of flips to guess: 1
guess the next 1 [ex: H]: H
You guessed: H
   Flip Seq: T
Nope...try again.
------------------------------
guess the next 1 [ex: H]: H
You guessed: H
   Flip Seq: T
Nope...try again.
------------------------------
guess the next 1 [ex: H]: H
You guessed: H
   Flip Seq: H
You did it! You correctly guessed the sequence!!
$ python guessNflips.py
number of flips to guess: 4
guess the next 4 [ex: HHHT]: HHTH
You guessed: HHTH
   Flip Seq: TTTT
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: TTTT
You guessed: TTTT
   Flip Seq: HHHT
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: HHTH
You guessed: HHTH
   Flip Seq: HTTT
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: THTH
You guessed: THTH
   Flip Seq: TTTH
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: THHT
You guessed: THHT
   Flip Seq: HHTH
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: HHHT
You guessed: HHHT
   Flip Seq: TTHT
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: TTHT 
You guessed: TTHT
   Flip Seq: HTTH
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: TTHH
You guessed: TTHH
   Flip Seq: HTTH
Nope...try again.
------------------------------
guess the next 4 [ex: HHHT]: THTH
You guessed: THTH
   Flip Seq: THTH
You did it! You correctly guessed the sequence!!

Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.