Lab 3: Advanced For Loops, Conditionals and Random Module

Goals

After the lab, you should be proficient at

  1. solving advanced problems using for loops
  2. solving problems that require making decisions
  3. using functions available in the random module

Linux: Preparing for Lab

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 lab3.1.py through lab3.8.py.

Your programs will be graded on correctness, style, 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 lab3.x.out, where x is the problem number.

  1. Challenge Problem. (15) The Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, ... The pattern is that the nth number is Fn=Fn-1 + Fn-2 for n greater than 1. The sequence is defined as F0=F1=1. Write a program that computes the first 15 numbers in the Fibonacci sequence.

    If you're having difficulty solving this problem, think about: How many times does this loop need to execute? What needs to be repeated? Try solving this problem by hand, calculating and writing out the results. You won't receive any help until we see that you have something written out. Hint: this is a modification of the accumulator design pattern.

    You can move on and then come back to this problem.

  2. (10) Write a program that asks the user to enter two values: the width and height of a box. Your program should then draw a box of dots (periods) in the terminal window with the given width and height.

    Example Output:

    This program draws a box.
    
    Enter width (2-80): 15
    Enter height (2-20): 5
    
    ...............
    .             .
    .             .
    .             .
    ...............
    
  3. (10) Write a program that takes as input two numbers--the first is the Steelers' score for the Super Bowl at the end of regulation play and the second is the Packers' score at the end of regulation. Then use only if statements (no elses or elifs) to print "The Steelers win" if the first number is bigger, "The Packers win" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal.
  4. (15) 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 of the old version and the new version. (The efficiency of the two programs is likely the same.)
  6. (10) Write a program that takes a number as input and prints ``Eureka!'' if the number is between 500 and 1000, inclusive; otherwise, print ``Your number (<the number>) is out of range.'' (Fill in the user's number for <the number>.)
  7. (5) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  8. (25) Application. Write a simple Super Bowl simulation program. You'll perform 10 simulations and keep track of how many games the Steelers win and how many games the Packers win. 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 -8 and 5, inclusive. If the generated number is positive, the Steelers (the first team/AFC team) win. Otherwise, the Packers (the second team/NFC team) win.

    Style. You should be able to easily modify this program to run next year (when the Dolphins and the Panthers are in the Super Bowl...). 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 times you simulate the game.

    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:

    Simulated Game 1 Steelers won
    Simulated Game 2 Packers won
    Simulated Game 3 Steelers won
    Simulated Game 4 Steelers won
    Simulated Game 5 Steelers won
    Simulated Game 6 Packers won
    Simulated Game 7 Packers won
    Simulated Game 8 Steelers won
    Simulated Game 9 Steelers won
    Simulated Game 10 Packers won
    ------------------------------
    The Steelers are predicted to win 6 out of 10 times.
      
    Simulated Game 1 Steelers won
    Simulated Game 2 Packers won
    Simulated Game 3 Steelers won
    Simulated Game 4 Steelers won
    Simulated Game 5 Packers won
    Simulated Game 6 Packers won
    Simulated Game 7 Packers won
    Simulated Game 8 Packers won
    Simulated Game 9 Steelers won
    Simulated Game 10 Steelers won
    ------------------------------
    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. Copy your lab3 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.

    Again, 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 1:20 p.m. on Friday.

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

Grading (100 pts)