After the lab, you should be proficient at
for
loopsrandom
moduleAs usual, create a directory for the programs and output you develop in this lab.
We'll practice writing several Python programs, each in their own text file. Name the files lab3.1.py through lab3.8.py.
Your programs will be graded on correctness, 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, 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 lab3.x.out, where x is the problem number.
If you're having difficulty solving this problem, think about: How many times does this loop need to execute? What needs to be repeated? Try solving this problem by hand, calculating and writing out the results. You won't receive any help until we see that you have something written out. Hint: this is a modification of the accumulator design pattern.
You can move on and then come back to this problem.
for
loops, a variable with
value "She loves you," and another variable with value "yeah", print
out the Beatles lyrics:
She loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah Yea-aahh
(Note: What type of problem is this?)
elses
(no elif
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.not
and a pair of parentheses to reverse the behavior of your
program.To determine the winner for a game, generate a random number between -13 and 6, inclusive. If the generated number is positive, the Saints (the first team) win. Otherwise, the Colts (the second team) win.
Style. You should be able to easily modify this program to run next year (when the Dolphins and the Packers 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've been 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 implementation.
Example Runs:
Simulated Game 1 Saints won Simulated Game 2 Colts won Simulated Game 3 Saints won Simulated Game 4 Saints won Simulated Game 5 Saints won Simulated Game 6 Colts won Simulated Game 7 Colts won Simulated Game 8 Saints won Simulated Game 9 Saints won Simulated Game 10 Colts won ------------------------------ The Saints are predicted to win 6 out of 10 times.
Simulated Game 1 Saints won Simulated Game 2 Colts won Simulated Game 3 Saints won Simulated Game 4 Saints won Simulated Game 5 Colts won Simulated Game 6 Colts won Simulated Game 7 Colts won Simulated Game 8 Colts won Simulated Game 9 Saints won Simulated Game 10 Saints won ------------------------------ The simulation is inconclusive.
turnin
directory.
(Review the UNIX handout if you don't
remember how to do that.)
printLab.sh
command.
Again, you should probably print from the labs
directory.
Print the file using the lpr
command introduced in the
first lab.
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!