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

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.6.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. (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 would you solve 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.)
  2. (20) Write a program that displays the multiplication tables from 0 to 12.

    Example output:

     x |   0 |   1 |   2 |   3 |   4 |    5 |   6 |   7 |   8 |   9 |  10 |  11 |  12 |
    -----------------------------------------------------------------------------------
     0 |   0 |   0 |   0 |   0 |   0 |    0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
     1 |   0 |   1 |   2 |   3 |   4 |    5 |   6 |   7 |   8 |   9 |  10 |  11 |  12 |
     2 |   0 |   2 |   4 |   6 |   8 |   10 |  12 |  14 |  16 |  18 |  20 |  22 |  24 |
    ...
    12 |   0 |  12 |  24 |  36 |  48 |   60 |  72 |  84 |  96 | 108 | 120 | 132 | 144 |
    
  3. (10) Using two for loops, a variable with value "She loves you," and another variable with value "yeah", print out the Beatles lyrics:
    She loves you, yeah, yeah, yeah
    She loves you, yeah, yeah, yeah
    She loves you, yeah, yeah, yeah
    Yea-aahh
    
  4. (10) Write a program that reads in two numbers--the first is the New England Patriots' score for the Super Bowl at the end of regulation play and the second is the New York Giants' score at the end of regulation. Then use only if statements (no elses) to print "The Patriots win" if the first number is bigger, "The Giants win" if the second number is bigger, and "They tied! We're going to overtime!" if the numbers are equal.
  5. (15) Copy the previous program and modify it so that it uses elses. 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.
  6. (30) Write a simple Super Bowl simulation program. You'll perform 10 simulations and keep track of how many games the Patriots win and how many games the Giants win. In the end, you'll print out the overall winner, based on your simulation.

    To determine the winner for a game, generate a random number between -8 and 20, inclusive. If the number generated is positive, the Patriots (the first team) win. Otherwise, the Giants (the second team) win.

    Style. 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. Using constants makes your program more generalizable.

    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.

    Example Runs:

    Simulated Game 1 Patriots won
    Simulated Game 2 Giants won
    Simulated Game 3 Patriots won
    Simulated Game 4 Patriots won
    Simulated Game 5 Patriots won
    Simulated Game 6 Giants won
    Simulated Game 7 Giants won
    Simulated Game 8 Patriots won
    Simulated Game 9 Patriots won
    Simulated Game 10 Giants won
    ------------------------------
    The Patriots are predicted to win 6 out of 10 times.
      
    Simulated Game 1 Patriots won
    Simulated Game 2 Giants won
    Simulated Game 3 Patriots won
    Simulated Game 4 Patriots won
    Simulated Game 5 Giants won
    Simulated Game 6 Giants won
    Simulated Game 7 Giants won
    Simulated Game 8 Giants won
    Simulated Game 9 Patriots won
    Simulated Game 10 Patriots 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)