Comp 150-001, MWF 12:35, Crown 105

Week 5


Exam 1: Wednesday, Feb 20

You will get a study guide. You will also get a one-page Python summary that will also be available to you during the exam.


Homework 4, due Monday, Feb 18

You are to submit one file containing the functions necessary for the following:


Homework 4

1. Draw a face, using the graphics module graphics.py. The face should have a circle (or oval) head, two eyes, a nose, and a mouth. The mouth (or some other feature) should be a non-Circle (eg a Polygon). Ears are optional. Actually a face is optional: any picture will do but it should have at least six components of at least two (preferably three) different types. Try to use color too. Put your drawing steps into a function, like this. (In this example, the GraphWin object is a parameter, but you can also put the GraphWin creation into the function.)

def drawface(w):
   head.draw(w)
   lefteye.draw(w)
   righteye.draw(w)
   nose.draw(w)
   mouth.draw(w)

One trick that's handy is to pick a center point, c=Point(200, 200), and then work out all the other points relative to your center:
    lefteyep = c.clone()
    lefteyep.move(-50, -20)
    hi = Text(Point(200, 100), "hello there")
    lefteye = Circle(lefteyep, 10)
I find it easiest to clone the center point like this, but you can also clone the left eye and move it right by twice as much horizontally (100) and nothing vertically (0). Or skip cloning, and just do everything "from scratch".

2. In the original madlibs, we had to know in advance the words (animal, food, city) that we wanted to replace.

How can we write a madlibs to take a story string with embedded {keywords}, extract all of them with string operations, prompt the user for those, and then display the madlibs result?

Here is a getKeys().

def getKeys(formatString):
    '''formatString is a format string with embedded dictionary keys.
    Return a list containing all the keys from the format string.'''

    keyList = list()
    end = 0
    repetitions = formatString.count('{')
    for i in range(repetitions):
        start = formatString.find('{', end) + 1
        end = formatString.find('}', start)
        key = formatString[start : end]
        keyList.append(key)
    return keyList

originalStory = """
Once upon a time, deep in an ancient jungle,
there lived a {animal}.  This {animal}
liked to eat {food}, but the jungle had
very little {food} to offer.  One day, an
explorer found the {animal} and discovered
it liked {food}.  The explorer took the
{animal} back to {city}, where it could
eat as much {food} as it wanted.  However,
the {animal} became homesick, so the
explorer brought it back to the jungle,
leaving a large supply of {food}.

The End
"""

print(getKeys(originalStory))

One catch with getKeys is that some cues may be duplicated. Here is one approach:


    d = dict()
    for cue in getKeys(originalStory):
       if cue in d: continue             # if the cue is already defined, don't do it again!
       cuevalue = input("give me a value for {}".format(cue))
       d[cue] = cuevalue
    print(originalStory.format(**d))


Monday:

graphics.py

    installing it: install it in a system directory, or in the same folder as your project.

    using it

Features:

Actions:

from graphics import *
from graphics import *

def draw1():
    w = GraphWin("my window", 400, 400)
    center=Point(200,200)
    circle=Circle(center, 100)
    circle.draw(w)
    circle.setFill("cyan")
    # other colors too: "red", "red3", "purple", "green4", "cyan2", "magenta"
    # Is there a "cerulean"?
    rect = Rectangle(Point(100, 50), Point(300, 150))
    rect.draw(w)
    rect2 = rect.clone()
    rect2.move(0, 200)      # which way does it move?
    rect2.draw(w)
    hi = Text(Point(200, 100), "hello there")
    hi.draw(w)
    lefteyept = center.clone()
    lefteyept.move(-50, -20)
    lefteye = Circle(lefteyept, 10)
    lefteye.draw(w)
    righteyept = center.clone()
    righteyept.move(-50, +20)
    righteye = Circle(righteyept, 10)
    righteye.draw(w)
    tr1 = Polygon(Point(100,100), Point(100, 150), Point(150,150))
    tr1.draw(w)
    tr1.setFill("purple")

    plist = []
    plist.append(Point(160, 300))
    plist.append(Point(240, 300))
    plist.append(Point(240, 340))
    plist.append(Point(280, 340))
    plist.append(Point(280, 380))
    plist.append(Point(200, 380))
    plist.append(Point(200, 340))
    plist.append(Point(160, 340))
    poly = Polygon(plist)
    poly.draw(w)
    poly.setFill("magenta")

    plist = []
    plist.append(Point(0, 0))
    plist.append(Point(80, 0))
    plist.append(Point(80, 40))
    plist.append(Point(120, 40))
    plist.append(Point(120, 60))
    plist.append(Point(40, 60))
    plist.append(Point(40, 20))
    plist.append(Point(0, 20))
    poly2 = Polygon(plist)
    poly2.move(40, 320)
    poly2.setFill("cyan3");
    poly2.draw(w)


draw1()

Wednesday:

study guide

triangles, polygons