Lab 5: Conditionals
Goals
After the lab, you should be proficient at
- solving problems that require making decisions
- solving advanced conditional problems
- solving advanced problems using
for
loops and conditionals
Objective: Review
Review the slides for today.
Objective: Set Up
As usual, create a directory for the programs and output you
develop in this lab in your cs111
directory.
Run runHelpClient
Objective: Programming in Python
We'll practice writing several Python programs, each in their own
text file. Name the
files lab5_1.py
, lab5_2.py
, etc.
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" by running the program again
(using F5), demonstrate that the program works using
several good test cases, and save the output to a file
named lab5_x.out
, where x is the problem number.
- (10) Write a program that takes as input two numbers--the first is
the W&L's score at the end of regulation and the second is VMI's
score at the end of regulation. Then
use only
if
statements (no elses or elifs) to print "W&L wins!" if the first number is bigger, "VMI wins!" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal. - (10) Copy the previous program and modify it so that it uses
elses
(noelif
s). Is this version better or worse than the previous version? Think about how much work the computer has to do (i.e., how many comparisons the computer makes) and the control flow diagram (performance), and how easy it is for a human to understand what is going on (readability). Write your thoughts in comments. - (10) Copy the previous program and modify it to use
if-elif-else
structure instead. In comments, compare the readability and efficiency of the old version and the new version. - (10) Write a program that takes a number as input and prints "Eureka!" if the number is between 500 and 1000, inclusive; otherwise, print "<the number> is out of range." (Fill in the user's number for <the number>.)
- (10) Copy the previous problem and add only a
not
and a pair of parentheses to reverse the behavior of your program. - (20) Revisit your program for distributing the tracks on
Greatest Hits Albums (lab2.3.py). Make a copy of the program and
save it in your
lab5
directory aslab5_6.py
.Update the program to give more intuitive output, for example:
- There doesn't need to be any output about tracks waiting for the next album if there aren't any tracks remaining.
- The output for the cds should say "cd" or "cds", as appropriate.
- If the user enters that there are no hits/tracks or a negative number of hits/tracks, the program should print an error message.
This is a surprisingly complex program in terms of the error cases. Try to keep your code as simple/straightforward as possible. It's hard to know what "straightforward" means so early in programming, but if you feel the code is getting complex (long lines) and hard to follow, take a step back and analyze the problem again.
Note that this is a good way to develop your program: first handle the "normal" cases and make sure that works. Then, modify your code to handle the error cases appropriately and the format peculiarities.
Below are some example runs:
How many greatest hits/tracks do you have? 0 Error: The number of tracks must be greater than 0.
How many greatest hits/tracks do you have? 5 How many tracks fit on a cd? 0 Error: The number of tracks that fit on a cd must be greater than 0.
How many greatest hits/tracks do you have? 5 How many tracks fit on a cd? 5 Your album requires 1 cd.
How many greatest hits/tracks do you have? 5 How many tracks fit on a cd? 6 Your album requires 0 cds. 5 tracks will need to wait for the next Greatest Hits album.
How many greatest hits/tracks do you have? 6 How many tracks fit on a cd? 5 Your album requires 1 cd. 1 track will need to wait for the next Greatest Hits album.
How many greatest hits/tracks do you have? 12 How many tracks fit on a cd? 5 Your album requires 2 cds. 2 tracks will need to wait for the next Greatest Hits album.
- Championship Simulation (30 pts). You are
going to write a March Madness Championship--the NCAA men's and
women's basketball championships--simulation. (If you don't like
basketball, this will work for most competitions.) Specifically,
this will perform a simulation of one game within the championship
series (e.g., the final championship game). You'll perform
multiple simulations and keep track of how many games each team
wins. In the end, you'll print out the overall winner (who won the
most games), based on your simulation.
- Overview.
- To determine the winner for a simulated game, generate a random number between -9 and 12, inclusive. If the generated number is positive, the favored team wins. Otherwise, the underdog team wins. (The number range is loosely based on the expected difference between the teams' scores.)
- Style.
- You should be able to easily modify this program to run for other championships (e.g., in another year or a different tournament). As you probably recall, using constants makes your program more generalizable and flexible. For full credit, you must use constants for the name of each team, the minimum and maximum difference values, and the number of simulated games played.
- Functions.
- Write a function called
hasFavoredTeamWon()
that takes no parameters and returnsTrue
if the favored team wins. Otherwise, the function returnsFalse
. Review the "Boolean Functions" section of your textbook. Note that you cannot programmatically test this function.The rest of your (non-constants) code will be in a
main
function. - Demonstration.
- Run your program at least once with 20 simulated games, which should require only one change to your program. Then, run your program again with 10 simulated games multiple times to show all the different possible outcomes.
- Analysis.
- You're only a few weeks into programming,
but you're already able to write a basic game simulator.
In comments at the top of the program (after listing yourself
as author), answer the following questions:
- Consider if you decided to change how the winning team of one simulation is generated. With the current code organization, how much/where does the code need to change? What won't need to change? Why is this a good design? (If you're struggling to answer this question, we may need to check that your code is organized appropriately.)
- Now that you're thinking about changing the code, describe 2 extensions you could make to this program to make the program "better", e.g., make it a more sophisticated simulation or more customizable or easier to use. What would be required for you to be able to implement those extensions? Time to implement it? Programming knowledge? Be specific about what constructs/knowledge you'd need to write your extended version.
Example Runs: with Duke as the favored team and UNC as the underdog team:
Simulation 1 : Duke wins Simulation 2 : Duke wins Simulation 3 : UNC wins Simulation 4 : Duke wins Simulation 5 : Duke wins Simulation 6 : UNC wins Simulation 7 : Duke wins Simulation 8 : UNC wins Simulation 9 : UNC wins Simulation 10 : Duke wins Duke is predicted to win 6 out of 10 times
Simulation 1 : Duke wins Simulation 2 : UNC wins Simulation 3 : Duke wins Simulation 4 : Duke wins Simulation 5 : UNC wins Simulation 6 : Duke wins Simulation 7 : UNC wins Simulation 8 : UNC wins Simulation 9 : UNC wins Simulation 10 : Duke wins The simulation is inconclusive.
The next 3 programs are about a hypothetical basketball game between W&L and VMI. Do not use functions to solve these problems.
Pause: Discuss the above answers with a student assistant or the instructor before moving on.
Finishing up: What to turn in for this lab
- IDLE and jEdit may create backup files with the "~" extension. Delete these files to declutter your printable lab.
- Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Submit
your lab directory into your
turnin
directory. - Log out of your machine when you are done.
Note that each command links to a page with more information about using the command.
Labs are due at the beginning of Friday's class. The electronic
version should be in the turnin
directory before
class on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown