This lab assignment requires you to write more programs in python. As always, 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/04We will only grade files submitted by handin21 in this labs directory, so make sure your programs are in this directory!
To receive full credit on the lab, your program needs to produce the correct output and be clear and readable. Remember to:
In lab2 you wrote a fundraising program that continued for a fixed number of days. For this lab, we will revisit that program, but instead continue the fundraising program until the goal is met or surpassed.
Here is a sample run of the program when the goal is surpassed:
$ python fundraiser2.py What is the fundraising goal? $100 How much was raised on day 1? $50 Total raised so far: $50 We are 50% of the way to the goal of $100 How much was raised on day 2? $25 Total raised so far: $75 We are 75% of the way to the goal of $100 How much was raised on day 3? $15 Total raised so far: $90 We are 90% of the way to the goal of $100 How much was raised on day 4? $40 Total raised so far: $130 We are 130% of the way to the goal of $100 We EXCEEDED our goal of $100 and raised $130 in 4 days!
Here is a sample run of the program when the amount raised is equal to the desired goal:
$ python fundraiser2.py What is the fundraising goal? $500 How much was raised on day 1? $300 Total raised so far: $300 We are 60% of the way to the goal of $500 How much was raised on day 2? $200 Total raised so far: $500 We are 100% of the way to the goal of $500 We reached our goal of $500 in 2 days!
HINT: When you are using print formatting, if you want an actual percent sign to appear in the string, you need to put two percent signs in succession. For example:
$ python >>> score = 95 >>> print "Score was %d%%" % (score) Score was 95%
This program will 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. If they are different, then Player A gets both coins. If they are the same, then Player B gets both coins. This is one round of the game. 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 This is a Coin War! How many coins for each player? 3 Round 1 Player A: 3 Player B: 3 Coin A: Heads Coin B: Tails Different--->A wins Round 2 Player A: 4 Player B: 2 Coin A: Tails Coin B: Heads Different--->A wins Round 3 Player A: 5 Player B: 1 Coin A: Heads Coin B: Heads Same-------->B wins Round 4 Player A: 4 Player B: 2 Coin A: Tails Coin B: Heads Different--->A wins Round 5 Player A: 5 Player B: 1 Coin A: Tails Coin B: Heads Different--->A wins *** Player A 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 python. 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!
The program asks the user to enter the number of coins before starting the game. Let's update the program so that it now ensures that the user enters at least 1 and no more than 25. You do not need to create a new program, just edit the same program (coinwar.py) to include this additional feature.
Here is a sample run of the updated program:
$ python coinwar.py This is a Coin War! How many coins for each player? 50 You must select no more than 25 coins, try again. How many coins for each player? 0 You must select at least 1 coin, try again. How many coins for each player? 1 Round 1 Player A: 1 Player B: 1 Coin A: Heads Coin B: Heads Same-------->B wins *** Player B wins the game! ***
HINT: You can use a while True loop around the input
statement that uses a break to exit the loop once valid
input is given.
The main function for the Coin war problem has gotten quite long now. Let's add a helper function to our program so that we can shorten the main function, making it clearer and easier to understand.
Edit the coinwar.py progam. Create a function called showResults that takes two parameters called playerAcoins and playerBcoins. Once the game is over, it will compare the number of coins each player has and report the winner.
In the main function, remove the original code that printed the winner and replace it with a call to your new showResults function.
Note that the output of the program should not change at all based
on this enhancement. We've simply made the program clearer.
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt.
You may run handin21 as many times as you like. We will grade the most recent submission submitted prior to the deadline.
Remember: for this lab, programs must appear in your cs21/labs/04 directory. If you create your programs in a different directory, use the unix mv command to move them into the cs21/labs/04 directory. For example:
mv myprog.py ~/cs21/labs/04/