CS21 Lab 4: while loops and functions

Due Saturday (October 1) before midnight

For this assignment you will write three programs. All of them use while loops. Only the last one requires you to write functions (besides main()).

Remember, we only grade files submitted by handin21 in your cs21/labs/04 directory, so make sure your programs are in that directory!



Programming tips

As your programs become larger, it is even more important to develop good habits:

The above are mandatory and you will be graded on them.

We strongly advise you to write your programs incrementally and test them as you go. No one writes interesting programs all at once. As you write, test that your program does what you expect (even if it doesn't yet do what you want). If it surprises you, make sure you understand what's happening before writing more code.

We have provided some test input and output; make sure you test your program! You should also test your program using some inputs and outputs that were not shown here; after all, we will. Come up with your own test cases and verify that the program is producing the right output on them.



1. Fake Download Status Bar
download

Write a program called download.py that displays a download status bar (hash marks) to the terminal screen. Your program should ask for the size of the file to download (in MB), and then display the status of the download as shown below. Since we aren't really downloading a file, we will simulate this with a while loop and the random library. Assume the file is downloaded in chunks, and the size of each chunk is chosen randomly to be either 0MB, 20MB, 40MB, or 60MB.

Each time another chunk of the file is downloaded, your status bar should update the display (on the next line). The program should continue downloading until 100% of the (fake) 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% (  40MB)
0% ############                                       100% ( 100MB)
0% ############                                       100% ( 100MB)
0% ###############                                    100% ( 120MB)
0% #################                                  100% ( 140MB)
0% #################                                  100% ( 140MB)
0% ####################                               100% ( 160MB)
0% ####################                               100% ( 160MB)
0% #########################                          100% ( 200MB)
0% #########################                          100% ( 200MB)
0% #########################                          100% ( 200MB)
0% #########################                          100% ( 200MB)
0% ###########################                        100% ( 220MB)
0% ###################################                100% ( 280MB)
0% ########################################           100% ( 320MB)
0% ###############################################    100% ( 380MB)
0% ################################################## 100% ( 400MB)

$ python download.py
size of download in MB: 130

0%                                                    100% (   0MB)
0% #######################                            100% (  60MB)
0% ##############################                     100% (  80MB)
0% ##############################                     100% (  80MB)
0% ################################################## 100% ( 130MB)

Requirements for this program:

Hint: to figure out how many hash marks to display, figure out the percentage downloaded so far, then multiply by 50. For example, if 100MB out of 130MB have been downloaded, that's 76.9% * 50 hash marks, or 38.46 hash marks. Obviously we can't display 38.46 hash marks in the terminal window, so converting that to an integer (38) is good enough.

Another Hint: to display a percent sign, use two in a row in the string. For example:

>>> x = 76
>>> print("%d%%" % (x))
76%


2. Coin War
coinflips

Write a program called coinwar.py to simulate a two-player game called Coin War. Player A and Player B start with an equal number of coins. In each round, two coins are flipped:

The game continues, round by round, until one of the players has won all of the coins.

Here is a sample run of the program:

$ python coinwar.py

Coin War Simulator...

How many coins for each player? 3

Round  1:  A=3   B=3
 -- Tails Tails --
----> SAME: B wins

Round  2:  A=2   B=4
 -- Heads Tails --
----> DIFFERENT: A wins

Round  3:  A=3   B=3
 -- Tails Tails --
----> SAME: B wins

Round  4:  A=2   B=4
 -- Heads Heads --
----> SAME: B wins

Round  5:  A=1   B=5
 -- Heads Heads --
----> SAME: B wins

**** Player B wins the game! ****

You can use the choice function from the random library to simulate flipping a coin. The choice function takes a list of possibilities and randomly selects one of the options. For example, choice(['rock', 'paper', 'scissors']) might return 'paper' one time and then 'rock' the next time. Try it in the python interactive shell! Remember to import the random library at the top of your file.

Because you will be using a random function to simulate the coin flips, your program will potentially generate a different outcome every time you run it!

Requirements for this program:



3. Slot Machine
slots

For this one we will write a program called slotmachine.py to simulate playing a slot machine. Our slot machine simulation has the following requirements and rules:

Here's part of a sample run of the slot machine simulation (but please read the instructions below before starting):

$ python slotmachine.py

------------- You currently have: $10
||cherry|| bell|| bar ||

L-O-S-E
 
------------- You currently have: $8
|| bar || bell|| bell||

two symbols match...you win $2
 
------------- You currently have: $8
||cherry|| bell|| bell||

two symbols match...you win $2
 
------------- You currently have: $8
||cherry|| bell||heart||

L-O-S-E
 
------------- You currently have: $6
||cherry||cherry||cherry||

winner! ($5)
 
------------- You currently have: $9
||cherry|| bar || bell||

L-O-S-E

------------- You currently have: $7
...
...

For this program, we want you to practice writing functions and incremental development: write a function, test it, write another function, test it, etc. Please follow the directions below.

Part A: write the pull() function

In a file called slotmachine.py, first write a pull() function to simulate one pull of the slot machine handle. Your pull() function should randomly choose one of "heart", "bar", "cherry", and "bell" for each of the three reels. After choosing for all three reels, your function should return a python list containing all reels.

For example, if the first reel came up "heart", the second "cherry", and the third "bell", your function should return the list ["heart","cherry","bell"].

Test your pull() function!

In main(), add some code to test your pull() function. A simple for loop that calls pull() and then prints the results is good enough:

for i in range(10):
  reels = pull()
  print(reels)

Note: this is only for testing. Once you are certain your function works, you can comment out or remove the test code from main().

Here's a sample of what the above test might look like:

$ python slotmachine.py
['cherry', ' bell ', ' heart']
['cherry', ' heart', 'cherry']
['cherry', ' bell ', '  bar ']
[' heart', 'cherry', 'cherry']
[' bell ', '  bar ', '  bar ']
[' heart', 'cherry', ' bell ']
['cherry', ' bell ', 'cherry']
['  bar ', ' bell ', 'cherry']
[' heart', ' heart', ' bell ']
[' heart', '  bar ', '  bar ']

Part B: write the winnings(reels) function

Given the reels (the python list with each reel's symbol), we should be able to determine the player's winnings (either $50, $5, $2, or $0 -- see requirements above). Write a function to determine and return the player's winnings, given the reels list.

Test your winnings() function!

As we did in Part A, add some code to main() to test your winnings() function. Make sure the winnings match the requirements above. Here's a sample output, printing the winnings and the reels:

$ python slotmachine.py
['  bar ', '  bar ', ' heart']
winnings = 2
[' heart', ' bell ', '  bar ']
winnings = 0
[' bell ', 'cherry', ' bell ']
winnings = 2
[' bell ', ' heart', ' bell ']
winnings = 2
['cherry', '  bar ', '  bar ']
winnings = 2
['  bar ', '  bar ', '  bar ']
winnings = 5
['  bar ', ' bell ', '  bar ']
winnings = 2
['  bar ', ' heart', ' bell ']
winnings = 0
[' heart', ' bell ', '  bar ']
winnings = 0
['cherry', '  bar ', ' heart']
winnings = 0

Part C: write the full main() function

Once you are certain the above two functions work, add some code to main() to run the full slot machine simulation. Your output should be similar to the sample above (show the result of pulling the handle, what they won (if anything), and adjust the player's money for the next round). The simulation should continue until either the player has $100, or they do not have enough to play again ($2).

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.