Lab 3: Conditionals and Random Module

Goals

After the lab, you should be proficient at

  1. solving problems that require making decisions
  2. solving indefinite loop problems
  3. using the 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.7.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. (10) Write a program that reads in two numbers. Then use only if statements (no elses) to print "Player 1 wins" if the first number is bigger, "Player 2 wins" if the second number is bigger, and "You tied!" if the numbers are equal.
  2. (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 (performance), and how easy it is for a human to understand what is going on (readability). Write your thoughts in the comments.
  3. (10) Write a program using the and operator in the condition for an if statement. Take a number as input and print ``Eureka!'' if the number is between 500 and 1000, inclusive; otherwise, print ``Your number (< the number >) is out of range.''
  4. (5) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  5. (15) The body mass index (BMI) is calculated as a person's weight (in pounds) times 720, divided by the square of the person's height (in inches). A BMI in the 19-25 range is considered healthy. Write a program that calculates BMI and prints a message telling whether they are above, within, or below the healthy range.
  6. (10) Write a program with a while loop that stops when the user enters a number that is evenly divisible by 6 (that is, there is no remainder). Print helpful output to the user.
  7. (35) Simulate a simple Craps game (without betting).

    The Rules

    1. A player rolls two dice. (How can you simulate rolling two dice?)
    2. There are three possibilities:
      • 7 or 11 wins. If the total of the dice is 7 or 11 then the player wins.
      • 2, 3, or 12 loses. If the total of the first roll is 2, 3, or 12 then the player loses.
      • Others become the point. If the total is any other number (4, 5, 6, 8, 9, 10) then this number becomes the point.

        The player keeps rolling until one of two things happen. Either the player makes the point and wins, or the player rolls a 7 and loses (craps out). Any number other than the point or 7 is of no consequence.

    After the first roll, print a message that tells the player that they either won, lost, or which "point" they have to play for.

    Example runs:

    *** This program simulates Craps (without the betting) ***
    
    You rolled a 11
    Congratulations!  You win!
    
    *** This program simulates Craps (without the betting) ***
    
    You rolled a 6
    The POINT is 6
    Roll again!
    
    You rolled 4
    Roll again!
    
    You rolled 6
    Congratulations!  You win!
    
    *** This program simulates Craps (without the betting) ***
    
    You rolled a 10
    The POINT is 10
    Roll again!
    
    You rolled 4
    Roll again!
    
    You rolled 9
    Roll again!
    
    You rolled 7
    Sorry!  You crapped out.  Luckily, money wasn't involved.
    

Extra Credit: Rainbow Dice (5 pts)

Simulate a game of Rainbow Dice, a Sprenkle family tradition. (Note that this will be fairly easy after you implemented the Craps game.)

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 2:25 p.m. on Friday.

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

Grading (100 pts)