echo $title ?>
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.
Run runHelpClient &
We're going to try to use this more so you don't have to have your hand up.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files lab4.1.py, lab4.2.py, etc.
Your programs will be graded on correctness, efficiency, style, 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, close 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 lab4.x.out, where x is the problem number.
- (10) Write a program that takes as input two numbers--the first
is the Falcons' score for the Super Bowl at the end of regulation
play and the second is the Patriots' score at the end of regulation.
Then use only
if
statements (no elses or elifs) to print "The Patriots win" if the first number is bigger, "The Falcons win" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal. - (15) 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 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.4.py). Make a copy of the program and
save it in your
lab4
directory. 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.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.
- Super Bowl Simulation (20 pts). You are going to write a
Super Bowl simulation. (If you don't like football, this will work for most
competitions.) 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.
To determine the winner for a game, generate a random number between -9 and 12, inclusive. If the generated number is positive, the Patriots (the first team/AFC team) win. Otherwise, the Falcons (the second team/NFC team) win. (This range is loosely based on the pre-game predictions.)
Output. Note the colon in the output below and how you can use the
print
statement or a string operation to yield that output.Style. You should be able to easily modify this program to run next year (when the Dolphins and the Lions are in the Super Bowl...). As discussed in class, 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 times you simulate the game.
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.
Extensions. You're only a few weeks into programming, but you're already able to write a basic game simulator. In comments in code, describe 3 extensions you would 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:
Simulated Game 1: Falcons won Simulated Game 2: Patriots won Simulated Game 3: Patriots won Simulated Game 4: Falcons won Simulated Game 5: Falcons won Simulated Game 6: Patriots won Simulated Game 7: Patriots won Simulated Game 8: Patriots won Simulated Game 9: Falcons won Simulated Game 10: Patriots won ------------------------------ The Patriots are predicted to win 6 out of 10 times.
Simulated Game 1: Falcons won Simulated Game 2: Patriots won Simulated Game 3: Falcons won Simulated Game 4: Falcons won Simulated Game 5: Patriots won Simulated Game 6: Patriots won Simulated Game 7: Patriots won Simulated Game 8: Patriots won Simulated Game 9: Falcons won Simulated Game 10: Falcons won ------------------------------ The simulation is inconclusive.
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 from your lab directory to save paper when you print.
- Copy your lab4 directory into
your
turnin
directory. - Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Print the file using
the
lpr
command. - Log out of your machine when you are done.
Perform the following steps from
your cs111
directory.
Note that
each command below links to a page with more information about
using the command.
You should hand in the printed copy and submit the electronic version before the beginning of Wednesday's class.
Grading (95 pts)
- Python programs: 95 pts; see above for breakdown