CS 111 w20 lecture 7 outline

1 Poll

x = 10
for change in range(5, 8, 1):
    if change > x / 2:
        x = x + change
    else:
        x = x - change
print(x)

2 Graphics example

Computers aren't just for helping us with mechanical or informational tasks, they can be aesthetic or creative tools as well. The code below is linked from the web page as art.py.

# for loop example using Portable Graphics Library
# requires pgl.py
# CS 111, Aaron Bauer

from pgl import GWindow, GOval # import what we need from Portable Graphics Library
import random
from random_color import random_hex_color

# these are constants, values set at the start of the program that should never change
# have all caps names by convention
GWINDOW_WIDTH = 500
GWINDOW_HEIGHT = 800

# first thing is to create the graphics window (GWindow) that will 
# display our graphics, and set its width and height
gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT)
circle = GOval(250, 250, 100, 100)
gw.add(circle)
# you would think that position (250, 250) would be the center of the window
# so why does the circle appear off center?
# (250, 250) is the center, but the position we give the circle is the location of its UPPER-LEFT CORNER

count = int(input("How many circles would you like? "))
# repeat the following code count number of times
for i in range(count):
    # create a circle at a random position
    # the x and y position we give the circle is the location of its upper-left corner
    # to make sure it stays within the window, limit x to 490 (window dimension of 500 - circle dimension of 10)
    # same idea for y
    circle = GOval(random.randint(0, 490), random.randint(0, 790), 10, 10)
    circle.setFilled(True) # make the circle filled in with color
    circle.setFillColor(random_hex_color()) # set the color filling in the circle randomly
    gw.add(circle) # the circle won't appear unless we add it to the GWindow

3 Practice

You are given GWINDOW_WIDTH, GWINDOW_HEIGHT, BALL_SIZE, use these to place a solid blue circle at the top right corner of the window.

4 List

  • A kind of value that contains other data
    • think of it as a slot in memory with its own numbered slots inside
    • the first slot is number 0!

4.1 Scrabble

  • Let's say we want a computer to help us play the word game Scrabble
  • We have a hand of tiles each with one letter
    • We'll represent this as a list: tiles = ["D", "O", "B", "Y", "E", "G", "O"]
    • We can access individual elements of a list with indexing (i.e., asking Python to retrieve the element at a particular numbered slot, or index)
    • print(tiles[0]) prints D
    • print(tiles[6]) prints O
    • print(tiles[7]) causes an IndexError
    • print(tiles[-2]) prints G
  • Lists can be modified by using this same index syntax of the left side of an assignment
tiles[2] = "L"
print(tiles) # prints ["D", "O", "L", "Y", "E", "G", "O"]
  • Lists are a kind of sequence, which means for loops can iterate over them just like range objects
tiles = ["D", "O", "B", "Y", "E", "G", "O"]
for tile in tiles:
    print(tile)

5 Poll

nums = [4, 5, 6]
for i in range(0, len(nums), 1):
    nums[i] = nums[i]**2
print(nums)

6 Loops within loops

  • Any code that can appear outside a for loop can appear inside a for loop, including another for loop
  • Useful in all kinds of places
from pgl import GWindow, GOval

GWINDOW_WIDTH = 500
GWINDOW_HEIGHT = 800
COLUMN_WIDTH = 50

histogram_data = [1, 0, 7, 1, 13, 1, 8]
gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT)

x = 0
for column in histogram_data:
    y = GWINDOW_HEIGHT - COLUMN_WIDTH
    for _ in range(0, column, 1):
        circle = GOval(x, y, COLUMN_WIDTH, COLUMN_WIDTH)
        circle.setFilled(True)
        circle.setFillColor("Red")
        gw.add(circle)
        y = y - COLUMN_WIDTH
    x = x + COLUMN_WIDTH

7 Practice

  • Write a function to sum the elements of a list and return the result

8 Quiz

  • Order of operations b - a / 2 does the division first
  • Average of 27.6, median of 29

status-week2.png