Lab 4: Conditionals, While Loops, and Object-oriented Programming

Policy Change: If you leave before lab is over (5:35) and have not completed the lab, there is no chance for an extension. Since two student assistants and the instructor are dedicated to helping you during the lab period, you should take advantage of that time to ask questions and get help.

Goals

After the lab, you should be proficient at

  1. solving advanced problems using conditionals
  2. using the exit function available in the sys module
  3. solving indefinite loop problems
  4. using objects and APIs

Linux

  1. As usual, create a directory for the programs and output you develop in this lab.
  2. Copy graphics.py from /home/courses/cs111/handouts/lab4/ into your lab4 directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab4.1.py through lab4.7.py.

Your programs will be graded on correctness, efficiency, 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, close 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) Copy lab3.5.py and modify it to use if-elif-else structure instead. In comments, compare the readability of the old version and the new version.
  2. (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 the BMI and prints a message telling whether they are above, within, or below the healthy range. If the user enters an unreasonable weight or height, print an error message and exit the program.
  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.'' (Fill in the user's number for <the number>.)
  4. (5) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  5. (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.
  6. (30) 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.
    

  7. (10) Using the graphics module, draw a yellow circle with radius 30 and a red square of width 50 in a window that is 400x200 with the name "Practice". Position the circle in the upper-left quadrant of the canvas and the square in the lower-right quadrant. (There will be no IDLE output for this program.)
  8. (10) Using the graphics module, draw the beginning of a snow-person. Create a canvas with the title "Snow Person". Draw a white circle of radius 50. Clone the circle and move the cloned circle above the first circle (so that the circle appears to be sitting on top of the original circle). Repeat with a third cloned circle. Draw two black, filled-in circles for eyes in the top circle. (Note that you should draw one eye, then clone the eye and move it to the appropriate place.) (There will be no IDLE output for this program.)

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 lab4 directory into the turnin directory.
  3. Delete the graphics.pyc file from your directory. Move graphics.py to the parent directory (labs). Otherwise, your print out will be huge and unreadable and the printLab.sh command will bomb.
  4. Create the document to print, using the printLab.sh command.

    View the file you just created using the gv command to ensure that you don't have any "junk" in the output.

    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.

However, it is best if you can finish this lab before you leave today to practice for Friday's midterm.

Grading (100 pts)