Lab 6: Practice with Lists

Goals

After the lab, you should be proficient at

  1. reading from and writing to files
  2. solving problems with lists
  3. using lists in functions

Linux

As usual, create a directory for the programs and output you develop in this lab.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab6.1.py through lab6.4.py.

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 lab6.x.out, where x is the problem number.

  1. (10) Write a program that uses two different techniques to create a list of length 5 that contains the values [1,2,3,4,5].

    Print out and label both lists.

  2. (15) Text Shorthand Generator. Create a text shorthand message for a phrase using the first letters from a phrase/sentence. (Hint: use one of the string methods to break the phrase into separate words.) Some example output:
    This program reads in a phrase and produces a text shorthand.
    
    Enter a phrase: This phrase doesn't stand for anything
    
    Shorthand is: tpdsfa
    
  3. (10) Create a list that contains the values 1--10. Print out the list in two columns that looks like (note the formatting):
    1      6
    2      7
    3      8
    4      9
    5     10
    
  4. (65) Deal or No Deal. Copy the files from /home/courses/cs111/handouts/lab6

    dealornodeal.handout.py contains the main function 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 parameters). Rename dealornodeal.handout.py as dealornodeal.py.

    Note that this program does not execute successfully.

    First, you should read through the program and get an idea of how it works. For example, how does the program keep track of the cases that have not been opened? How does the program keep track of the amounts that have not been revealed? (Why are these two different variables?) When does the game end?

    You should tackle the functions in this order, writing each one and then testing how far your program gets with each one.

    Testing: You will need to run this program several times to make sure it is working correctly.

Extra Credit (up to 10 pts)

Is it possible to make a better offer function? The banker wants to keep the offers low so that the players don't win too much money. However, if the offers are too low, the player will not take the offer and could win more money.

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 your function in comments.

Finishing up: What to turn in for this lab

  1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
  2. Copy your lab6 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Turn in your printed lab assignment, using the printLab.sh command. 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 2:25 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)