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.

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.

    We are preparing for the upcoming March Madness Tournament (that's men's and women's college basketball). We'll use two example teams that can easily be changed in the future, since we don't know who will be playing in the championship game of the tournament yet.

  1. (10) Write a program that takes as input two numbers--the first is the Duke's score at the end of regulation and the second is UNC's score at the end of regulation. Then use only if statements (no elses or elifs) to print "Duke wins!" if the first number is bigger, "UNC wins!" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal.
  2. (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.
  3. (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.

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


  5. (10) Write a program that takes a number as input and prints "Eureka!" if the number is between 500 and 1000, inclusive; otherwise, print "<the number> is out of range." (Fill in the user's number for <the number>.)
  6. (10) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  7. (20) Revisit your program for distributing the tracks on Greatest Hits Albums (lab2.2.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 surprisingly complex program in terms of the error cases. Try to keep your code as simple/straightforward as possible. It's hard to know what that means so early in programming, but if you feel the code is getting complex and hard to follow, take a step back and analyze the problem again.

    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. Championship Simulation (30 pts). You are going to write a March Madness Championship simulation. (If you don't like basketball, 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, Duke (the first team) wins. Otherwise, UNC (the second team) wins. (These numbers are loosely based on the expected difference between the teams' scores.)

    Functions
    We won't need to use functions, but you can put all of your code within a main function if you prefer.
    Style.
    You should be able to easily modify this program to run for other championships. 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.

    Demonstration. Run your program at least once with 20 simulated games, which should require only one change to your program. Then, run your program again with 10 simulated games.

    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:

    Simulation 1 : Duke wins
    Simulation 2 : Duke wins
    Simulation 3 : UNC wins
    Simulation 4 : Duke wins
    Simulation 5 : Duke wins
    Simulation 6 : UNC wins
    Simulation 7 : Duke wins
    Simulation 8 : UNC wins
    Simulation 9 : UNC wins
    Simulation 10 : Duke wins
    Duke is predicted to win 6 out of 10 times
    
    Simulation 1 : Duke wins
    Simulation 2 : UNC wins
    Simulation 3 : Duke wins
    Simulation 4 : Duke wins
    Simulation 5 : UNC wins
    Simulation 6 : Duke wins
    Simulation 7 : UNC wins
    Simulation 8 : UNC wins
    Simulation 9 : UNC wins
    Simulation 10 : Duke wins
    The simulation is inconclusive.
    

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)