knerr cs21 notes...
back to schedule
WEEK07: File I/O, top-down design
---------------------------------------------------------------
F: more top-down design
ANNOUNCEMENTS:
- Lab 7 due Tuesday Nov 3
- quiz 3 file: /home/jk/inclass/q3.py
(make sure you understand the quizzes!)
- /home/jk/inclass/wc.py is my wordcount solution from last class
TOP-DOWN DESIGN:
- let's try writing another program, using top-down design:
$ python rps.py
1. Rock
2. Paper
3. Scissors
your choice: 2
You chose Paper
*I* chose Paper
It's a T I E
$ python rps.py
1. Rock
2. Paper
3. Scissors
your choice: 1
You chose Rock
*I* chose Paper
I WIN! I WIN! I WIN!
- what high-level steps need to be done to play the
rock-paper-scissors game?
print the menu (1. rock, 2. paper, etc)
get the user's choice
make my choice
determine the winner
- the first step seems easy...write a function to print
out the above menu of choices
- the second step *can* be a one-liner, using input to
get an integer. Or, to be more robust, we could write
a getChoice function to get the user's choice and check
it to make sure it's valid. I would probably start with
the one-liner, just to see if things work, then later
add the getChoice function
- what kind of input do we want from the user? if they
want to choose Rock, should they enter a 1, or the word
"rock"?? This is a design choice you must make, and it
will determine how the rest of your code works
- the "make my choice" step can be done with randrange.
how do we randomly choose between 3 options?
- the "determine winner" step is a little harder. If the
user's choice and the computer's choice are integers from
1 to 3, how can we decide who wins? For example, suppose
the user chose 1 (rock) and the computer chose 2 (paper),
how do we figure out that the computer wins this game??
Hint: can you write out all possible outcomes, and come up
with an algorithm that determines the winner, based on the
user's choice and the computer's choice?
IF YOU HAVE TIME:
- write out rockpaperscissors.py
- make the "get user's choice" section robust!
- take a look at fileplot.py: how can you read in x y data
from a file?