Lab 5: Conditionals

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 labhelp

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.

You can use Python Visualizer to help you see what is happening in your program. This is the visualizer used in the text book.

    The next 3 programs are about a hypothetical basketball game between W&L and VMI. Sometimes, we write programs using functions. Sometimes we don't. Do not use functions to solve these problems. In fact, we won't write any functions until the last program.

  1. (10) Write a program that takes as input two numbers--the first is the W&L's score at the end of regulation and the second is VMI's score at the end of regulation. Then use only if statements (no elses or elifs) to print "W&L wins!" if the first number is bigger, "VMI wins!" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal.
  2. Carefully consider the test cases you use for your demo. Record the test cases so that you use the same test cases, in the same order, for the next two problems.

  3. (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 (i.e., how many comparisons the computer makes) 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.
  4. (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.

  5. Pause: Discuss the above answers with the instructor before moving on.


  6. (20) Revisit your program for distributing the tracks on Greatest Hits Albums (lab2_4.py). Make a copy of the program and save it in your lab5 directory as lab5_4.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 "straightforward" means so early in programming, but if you feel the code is getting complex (long lines) 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 (as in the previous lab). Then, modify your code to handle the error cases appropriately and the format peculiarities, as we're doing in this lab!

    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.
    

  7. (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>.)
  8. (10) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  9. Championship Simulation (30 pts). You are going to write a Super Bowl Championship simulation, so that you'll be ready for next year's game! (If you don't like American football, this will work for most competitions.) You'll perform multiple simulations of the championship game and keep track of how many simulated games each team wins. In the end, you'll print out your prediction of the winner, based who won the most simulated games. Read below for all the specifications.
    Functions.
    Write a function called hasFavoredTeamWon() that takes no parameters and returns True if the favored team wins (for one simulation). Otherwise, the function returns False. Review the "Boolean Functions" section of your textbook, which you read for today.

    To determine the winner for a simulated game, generate a random number between -9 and 12, inclusive. If the generated number is positive, the favored team wins. Otherwise, the underdog team wins. (The number range is loosely based on the expected difference between the teams' scores.)

    The rest of your (non-constants) code will be in a main function.

    Note that you cannot programmatically test this function. However, you could call the function several times and confirm that it either returns True or False.

    Style.
    You should be able to easily modify this program to run for other championships (e.g., in another year or a different championship). As you probably recall, 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 multiple times to show all the different possible outcomes.
    Analysis.
    You're only a few weeks into programming, but you're already able to write a basic game simulator. In comments at the top of the program (after listing yourself as author), answer the following questions:
    1. Consider if you decided to change how the winning team of one simulation is generated. (While the API for the function remains the same, consider big changes to the implementation of the function. The actual changes don't matter.) With the current code organization, how much/where does the code need to change? What won't need to change? Why is this a good design? (If you're struggling to answer this question, we may need to check that your code is organized appropriately.)
    2. Now that you're thinking about changing the code, describe 2 extensions you could 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: with my cat's prediction for next year's Super Bowl, the Cincinnati Bengals as the favored team and Detroit Lions as the underdog team (but my cat objects to the term "underdog"):

    Simulation 1 : Cincinnati Bengals win
    Simulation 2 : Cincinnati Bengals win
    Simulation 3 : Detroit Lions win
    Simulation 4 : Cincinnati Bengals win
    Simulation 5 : Cincinnati Bengals win
    Simulation 6 : Detroit Lions win
    Simulation 7 : Cincinnati Bengals win
    Simulation 8 : Detroit Lions win
    Simulation 9 : Detroit Lions win
    Simulation 10 : Cincinnati Bengals win
    The Cincinnati Bengals are predicted to win 6 out of 10 times
    
    Simulation 1 : Cincinnati Bengals win
    Simulation 2 : Detroit Lions win
    Simulation 3 : Cincinnati Bengals win
    Simulation 4 : Cincinnati Bengals win
    Simulation 5 : Detroit Lions win
    Simulation 6 : Cincinnati Bengals win
    Simulation 7 : Detroit Lions win
    Simulation 8 : Detroit Lions win
    Simulation 9 : Detroit Lions win
    Simulation 10 : Cincinnati Bengals win
    The simulation is inconclusive.
    

Finishing up: What to turn in for this lab

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

  1. Create the printable lab assignment, using the createPrintableLab command.
  2. View your file using the evince command.
  3. Check that the PDF contains all (and only) the necessary files.
  4. Print the file from evince. You can print to other printers if there are issues with the computer science printers (which do not cost you anything to print computer science work).
  5. Submit your lab directory into your turnin directory.
  6. 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)