Skip to main content.

Goals

After the lab, you should be proficient at

Objective: Review

Review the slides for today.

Objective: Set Up

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

Copy test.py from your lab4 directory into your lab5 directory.

Run runHelpClient &

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab5.1.py, lab5.2.py, etc.

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

  1. (20) Refactoring revisited Create program similar in outline to lab4.1.py in which you have a function, in this case calculateFine that takes two parameters speed and speedLimit and returns the calcuated fine, based on the example programs we wrote in class. If the person is not speeding, the calculated fine is 0.

    In the main function, write tests of calculateFine, again following the example in lab4.1.py. How many tests should you include to make sure that your function is working?

  2. (10) Copy the previous program. Rename the main function to testCalculateFine function and include a doc string on that function. Create a new main function that prompts the user for the speed and the speed limit and displays an appropriate message: (a) either that the user is driving fine or (b) what the fine is. You should not modify your function; it works, right? The print statements should all be in the main.

    We are preparing for the upcoming World Series (that's baseball). We'll try out two example teams that can easily be changed in the future, since we don't know who will be playing in the Series yet.

  3. (10) Write a program that takes as input two numbers--the first is the Orioles' score after nine innings in the first game of the World Series and the second is the Padres' after nine innings. Then use only if statements (no elses or elifs) to print "The Orioles win" if the first number is bigger, "The Padres win" if the second number is bigger, and "They tied! We're going to extra innings!" if the numbers are equal.
  4. (10) Copy the previous program and modify it so that it uses elses (no elifs). 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.
  5. (10) Copy the previous program and modify it to use if-elif-else structure instead. In comments, compare the readability and efficiency of the old version and the new version.

  6. Pause: Discuss the above answers with a student assistant or the instructor before moving on.


  7. (20) Revisit your program for distributing the tracks on Greatest Hits Albums (lab2.1.py). Make a copy of the program and save it in your lab5 directory as lab5.6.py. Update the program to give more intuitive output. For example, there doesn't need to be any output about tracks waiting for the next album if there aren't any tracks remaining. The output for the cds should say "cd" or "cds", as appropriate. If the user enters that there are no hits/tracks or a negative number of hits/tracks, the program should print an error message.

    This is a deceptively complex program in terms of the error cases.

    Note that this is a good way to develop your program: first handle the "normal" cases and make sure that works. Then, modify your code to handle the error cases appropriately and the format peculiarities.

    Below are some example runs:

    How many greatest hits/tracks do you have? 0
    Error: The number of tracks must be greater than 0.
    

    How many greatest hits/tracks do you have? 5
    How many tracks fit on a cd? 0
    Error: The number of tracks that fit on a cd must be greater than 0.
    

    How many greatest hits/tracks do you have? 5
    How many tracks fit on a cd? 5
    
    Your album requires 1 cd.
    

    How many greatest hits/tracks do you have? 5
    How many tracks fit on a cd? 6
    
    Your album requires 0 cds.
    5 tracks will need to wait for the next Greatest Hits album.
    

    How many greatest hits/tracks do you have? 6
    How many tracks fit on a cd? 5
    
    Your album requires 1 cd.
    1 track will need to wait for the next Greatest Hits album.
    

    How many greatest hits/tracks do you have? 12
    How many tracks fit on a cd? 5
    
    Your album requires 2 cds.
    2 tracks will need to wait for the next Greatest Hits album.
    

  8. World Series Simulation (20 pts). You are going to write a World Series simulation. (If you don't like baseball, this will work for most competitions.) You'll perform multiple simulations and keep track of how many games each team wins. In the end, you'll print out the overall winner (who won the most games), based on your simulation.

    To determine the winner for a game, generate a random number between -9 and 12, inclusive. If the generated number is positive, the Orioles (the first team/American League team) win. Otherwise, the Padres (the second team/National League team) win. (These numbers are loosely based on the expected difference between the scores.)

    We're going to assume that the World Series always goes 7 games. Determine the winner of the series from running the simulation 7 times.

    Functions
    We won't need to use functions, but you can put everything within a main function if you prefer.
    Output.
    Actually, this ended up being harder than I wanted it to be... It's not bad, but it's a little tricky. Try it for extra credit (3 points): Note the colon in the output below and how you can use the print function's options to produce that output.
    Style.
    You should be able to easily modify this program to run next year (when the Orioles and the Pirates are in the World Series...). 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 simulated games played.
    Extensions.
    You're only a few weeks into programming, but you're already 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 extended version.

    Example Runs:

    Simulated Game 1: Orioles won
    Simulated Game 2: Padres won
    Simulated Game 3: Padres won
    Simulated Game 4: Orioles won
    Simulated Game 5: Orioles won
    Simulated Game 6: Padres won
    Simulated Game 7: Padres won
    ------------------------------
    The Padres are predicted to win 4 out of 7 games.
      
    Simulated Game 1: Orioles won
    Simulated Game 2: Padres won
    Simulated Game 3: Orioles won
    Simulated Game 4: Orioles won
    Simulated Game 5: Orioles won
    Simulated Game 6: Padres won
    Simulated Game 7: Orioles won
    ------------------------------
    The Orioles are predicted to win 5 out of 7 games.
      

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. Submit your lab5 directory into your turnin directory.
  3. Perform the following steps from your cs111 directory.

    Note that each command below links to a page with more information about using the command.

  4. Create the printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>
  5. View your file using the evince & command.
  6. Print the file using the lpr command.
  7. Log out of your machine when you are done.

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 class on Friday.

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

Grading (100 pts)