After the lab, you should be proficient at
Instead of creating the lab8 directory, copy the directory
/home/courses/cs111/handouts/lab8
into your
labs
directory, using the -r
option that you
usually use to turnin your code.
We'll practice writing several Python programs, each in their own text file. Name the files, as usual.
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 lab8.x.out, where x is the problem number.
append
the
values in a loop.range
function (in other words, only one line of code).Print out and label both lists.
This program reads in a phrase and produces a text shorthand. Enter a phrase: This phrase doesn't stand for anything Shorthand is: tpdsfa
This program reads in a phrase and produces a text shorthand. Enter a phrase: Laughing out loud Shorthand is: lol
2 12 4 14 6 16 8 18 10 20
Hint: what are the positions in the list for each displayed element?
dealornodeal.py
contains the main
function and some additional functions to play the game Deal or No
Deal. For this problem, you will fill in the code for the
functions to make the game work correctly. You
should not modify the
main
function, and you should not modify
the function headers (i.e., don't modify the number of
parameters).
Note that the given program will not execute successfully.
I encourage you to talk to another person in the class to try to figure the program out. However, the program you write should be your own.
First, you should read through the program and get an idea of how it works. For example, answer the following questions:
Tackle the functions in the following order, writing each one and then testing how far your program gets with each one.
readCaseValues(filename)
: read the amounts
contained in the cases from the file named by the parameter
filename
. The amounts are in a file, such as case_values.txt
or 7_million_cases.txt
(the files that you previously
copied into the directory deal_or_nodeal
). Save the amounts in a
list. (Note: what are the data types of the amounts?) Look at
the contents of the files to help you figure out how to read in the values.printCasesLeft(cases)
: print the cases that
haven't been chosen, i.e., the cases that the player can select from.
So that the user can read all the cases easily, break them up in rows
of length 4, in formatted columns, such as
Cases Left to Choose from: 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
printBoard(amounts)
: print the amounts left on the
board in two columns. The amounts that have been opened should still
be on the board, just marked as having been chosen. The left column
should contain the smaller amounts. Example output:
****************************** The Board: $ 0.01 ---- $ 1.00 $ 5000.00 $ 5.00 $ 10000.00 $ 10.00 $ 25000.00 ---- $ 50000.00 $ 50.00 $ 75000.00 $ 75.00 ---- $ 100.00 $ 200000.00 $ 200.00 ---- $ 300.00 ---- $ 400.00 $ 500000.00 $ 500.00 ---- $ 750.00 $1000000.00 ******************************
isValidChoice(cases, choice)
: return True iff the
choice is a valid choice (has not already been chosen and is a valid
case number)
getUserChoice(cases, prompt)
: Repeatedly prompts the
user for a case number until the user selects a valid case. Returns
the valid choice.
Testing: You will need to run this program several times to make
sure it is working correctly. Test using both of the files in the
deal_or_nodeal
directory. You can add additional test
files.
Keep track of how much players are winning/losing in aggregate in a
file called payout.dat
. We'll consider the payout
to be the difference between the player's case and the deal made. If
the player does not make a deal, the payout is 0.
Read in the payout from the file. (Start the payout file at 0.) At the end of the game, print the new total payout to the file. Doing this part (reading/writing a file) will earn you 5 extra credit points.
After you've played the game approximately 10 times, you have a pretty good idea of the payouts. (Admittedly, the payout depends on luck and how good your player is.)
Then, try writing a modified offer function and comparing that payout to the other payout. Justify the modifications to the function in comments.
turnin
directory. (Review
the UNIX handout if you don't remember how
to do that.)
.pyc
file before
printingprintLab.sh
command to create a file to print out. You
should probably print from the
labs
directory.
View the lab8.ps file using the gv
command,
e.g., gv lab8.ps
. You should not print out the
original data files or the generated summary files.
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!