CS 115 Pre-Lab 3 Instructions

Deadline: Tu 2/3/2015 at 7 AM


Part 1: Definite loops and accumulation

Use the first two sections of the Week 3 reading assignment as necessary to complete this section.

  1. Answer Question 1 in Moodle.
  2. In a new browser tab, visit the Online Python 3 Tutor and enter the following Python code:
    for i in range(3):
        print('Cupcakes!')
    Then click "Visualize execution."
  3. Click the "Forward" button to step through the code, instruction by instruction. Pay attention to how the variable i changes (shown on the right side of the screen).
  4. Using the tutor to help you, answer Questions 2 and 3 in Moodle. Pay attention to both the program output (below your code) and the global variables (to the right).

    Your answer to Question 3 should be a comma-separated list with spaces between the values.
    For example: 8, 9, 10

  5. Now click "Edit code" in the Python tutor and enter the following code:
    for j in range(2, 8):
        print('Marshmallows!')
    Visualize this code and answer Questions 4 and 5 in Moodle.
  6. Now try...
    for k in range(8, 2):
        print('Thin Mints (TM)!')
    Visualize this program and answer Questions 6 and 7 in Moodle.
  7. Now try...
    x = 5
    for i in range(1, 6):
        x = i
    Visualize this program and answer Question 8 in Moodle.
  8. Now try...
    x = 5
    for i in range(1, 6):
        x = x + i
    Visualize this program and answer Question 9 in Moodle.

Part 2: Graphics concepts

Read over the following graphics code:

"""
Program: CS 115 Lab 3
Author: Your name
Description: Using the graphics package, this program will draw a circle.
"""
from graphics import *


def main():
    window = GraphWin("Circles", 800, 800)

    center = Point(100, 200)         # create a point to serve as the center of the circle
    radius = 40                     
    circle = Circle(center, radius)  # create a circle centered at "center" with radius "radius"
    circle.setOutline('blue')        
    circle.draw(window)              # draw the circle in the window that we created earlier

    window.getMouse()                # wait for the mouse to be clicked in the window
    window.close()                   # close the window after the mouse is clicked in the window


main()

A few notes about this code...

The statement

window = GraphWin("Circles", 800, 800)

creates a window called window whose title is Circles and whose width and height are 800 pixels each (the first number is the width and the second one is the height).

To create a circle, we need to first create a Point to position its center. The variable center serves this purpose. Notice that the data types of graphics objects begin with upper-case letters, and we are using lowercase letters for the names of specific objects.

We also made the radius a variable, so that we can easily change it later if necessary. But our code:

center = Point(100, 200)
radius = 40
circle = Circle(center, radius)  # create a circle centered at "center" with radius "radius"

is equivalent to:

circle = Circle(Point(100, 200), 40)  # create a circle centered at (100, 200) with radius 40

For now, either option is fine.

After we create and configure our circle, we use the following statement to draw it into the window that we created earlier:

circle.draw(window) # draw the circle in the "window" that we created earlier

Without that statement, the circle will not appear in the window.

Before our program finishes, we need to close the window that we have created. We use the following statement for that purpose.

window.close()      # close the window after the mouse is clicked in the window.

However, if we put this right after the circle.draw(win), our program will draw the circle and then immediately close the window, before you get a chance to see what has been drawn in it. To prevent that, we add the following statement:

window.getMouse()   # wait for the mouse to be clicked in the window
window.close()   # close the window after the mouse is clicked in the window
The statement window.getMouse() forces the program to wait for the user to click the mouse in the window before it proceeds. With this statement, we can make sure that the objects that we intended to draw appear in the window before it closes.

Answer Question 10 in Moodle. Remember that the graphics coordinates work like this:
Graphics window coordinate system

Part 3: Submit the pre-lab (due Tu at 7 AM)

Review your answers, and then click the Next button at the bottom of the Moodle quiz. Once you do that, you should see something similar to this:
Quiz attempt summary

Click the Submit all and finish button. You MUST do this so that your writeup can be graded! Once you have submitted your quiz, you should see something similar to this at the top of your Moodle window. The important part is that the State shows up as Finished.
Quiz confirmation