Lab 7: Practice with Functions, Modules, and Files

Goals

After the lab, you should be proficient at

  1. organizing your code using functions
  2. creating and using your own modules
  3. reading from files
  4. basic problems using lists

Linux

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

Create a subdirectory called deal_or_nodeal. Copy all the files from /home/courses/cs111/handouts/lab7 into the deal_or_nodeal directory you created.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab7.1.py through lab7.6.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 lab7.x.out, where x is the problem number.

  1. (15) Copy the palindrome program from the last lab and modify it so that the palindrome functionality is in a function. The function you create should take as input a string and output whether the string is a palindrome. (What is the keyword for "output from a function?")

    Extra Credit: Can you make this implementation more efficient than the previous program's implementation? (Worth up to 5 pts extra credit. If you implemented something similar in the last lab, you can only earn 3 extra points.)

  2. (25) Copy the Caesar Cipher program and modify it so that you have at least two new functions. One function will handle the encoding. This function takes as parameters a string and a key and returns the encoded string. Modify your program to use this function. Put the driver into a main function.

    Then, modify your program to include a function called translateLetter that takes as parameters a letter and a key and returns the translated version of the letter. Call the translateLetter function in the encoding function you just wrote.

    In comments, briefly compare the readablity, reusability, and efficiency of this program with the original program from last lab.

  3. (20) Create a Python script/module called game.py that contains some useful game variables and functions.

    Constants:

    Assign the constants appropriate values.

    Functions:

    Save rainbowdice.py as lab7.3.py and modify it to use the game module, i.e., use game's constants and call game's functions.

    In comments, compare the readability of rainbowdice, with and without the module.

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

    Print out and label both lists.

  5. (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 a list of words and another method to make the resulting phrase all lowercase.) 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
    
    This program reads in a phrase and produces a text shorthand.
    
    Enter a phrase: Laughing out loud
    Shorthand is: lol
    
  6. (15) Write a program that reads the amounts contained in the cases for the game show Deal or No Deal. 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?) Put the code into a function called readCaseValues that takes the file name as a parameter and returns the list. Create an appropriate main function.

Extra Credit (up to 10 pts)

Implement another game of your choice using the game module. You can add more functions to the game module, as appropriate. The difficulty of the game will determine the amount of extra credit you will receive.

Since you already have a solution for Craps, writing another solution that uses the game module (and adding other functions, as appropriate) will earn you 5 extra credit points.

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 lab7 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Before printing, remove the game.pyc file from your labs/lab7 directory. Verify that you have only the .py files you wrote and the .out files you created in that directory (plus a directory called deal_or_nodeal).
  4. Use the printLab.sh command to create a file to print out. 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)