Difference between revisions of "CSC111 Python Programs Fall 2015"

From dftwiki3
Jump to: navigation, search
(Nov. 30, 2015)
(Nov. 30, 2015)
Line 2,546: Line 2,546:
 
<br />
 
<br />
 
<br />
 
<br />
<onlydft>
+
 
 
==rainyCambridge.py==
 
==rainyCambridge.py==
 
<br />
 
<br />
Line 2,552: Line 2,552:
 
# rainyCambridge.py
 
# rainyCambridge.py
 
# D. Thiebaut
 
# D. Thiebaut
# finds the rainiest month in recorded history
+
# This program finds the rainiest month and year
# for Cambridge.
+
# in Cambridge, UK.  The file is fetched from a
 +
# Web site (http://cs.smith.edu/~dthiebaut/UKTemperatures/)
 +
# where several text files are stored, including one
 +
# for Cambridge, called cambridgedata.txt.
 
# Information on how to retrieve URLs found at
 
# Information on how to retrieve URLs found at
 
# https://docs.python.org/3.0/library/urllib.request.html
 
# https://docs.python.org/3.0/library/urllib.request.html
 
# and http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string
 
# and http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string
  
import urllib.request
+
i
 +
 
 +
import urllib.request   # library for accessing Web pages
  
 
#--- some constants used by the program ---
 
#--- some constants used by the program ---
Line 2,565: Line 2,570:
  
 
def getTemperatureData( fileName ):
 
def getTemperatureData( fileName ):
     """ Get the contents of a the given file located at the BASEURL."""
+
     # fetches the file from the Website and returns it as
 
+
    # a long string.
 +
    # format of the file, with a sample of the lines:
 
     """Cambridge NIAB
 
     """Cambridge NIAB
 
Location: 5435E 2606N, 26 metres amsl
 
Location: 5435E 2606N, 26 metres amsl
Line 2,575: Line 2,581:
 
               degC    degC    days      mm  hours
 
               degC    degC    days      mm  hours
 
   1959  1    4.4    -1.4      20    ---    78.1
 
   1959  1    4.4    -1.4      20    ---    78.1
 +
  1961  1    6.2    1.0      5    55.9    50.5
 
   1961  2  10.3    4.3      0    50.4    65.9
 
   1961  2  10.3    4.3      0    50.4    65.9
  1961  3  13.7    3.3      1    5.2  170.4
 
  1961  4  14.9    6.2      0    44.5  101.0
 
  1973  6  21.2    9.7      0    60.6*  273.1
 
 
   2011  7  21.2    10.9      0    38.4*    ---
 
   2011  7  21.2    10.9      0    38.4*    ---
 
   2015  1    8.0    1.3      12    48.4*  ---    Provisional
 
   2015  1    8.0    1.3      12    48.4*  ---    Provisional
""" 
 
  
 +
 +
    """
 
     url = BASEURL + fileName
 
     url = BASEURL + fileName
 
     print( "Retrieving data from", url )
 
     print( "Retrieving data from", url )
Line 2,589: Line 2,594:
 
     return data
 
     return data
  
def getRainData( data ):
+
def main():
     #process each line, filter out invalid lines
+
     # get the string
     #and keep only the
+
    data = getTemperatureData( "cambridgedata.txt" )
     rainDays = []
+
 
    for line in data.split( "\n" ):
+
     # split string into lines
         line = line.strip()
+
     lines = data.split( "\n" )
         # skip blank lines
+
   
         if len( line )<=1:
+
 
 +
    # create empty list
 +
    L = []
 +
   
 +
    # loop through all the lines
 +
    for line in lines:
 +
 
 +
        # strip extra space and remove the * characters
 +
         line = line.strip().replace( '*', ' ' )
 +
       
 +
         # get rid of blank lines
 +
         if len( line ) <= 1:
 
             continue
 
             continue
         if not line[0] in ['2', '1']:
+
 
 +
        # get preamble
 +
         if not line[0] in ['1', '2']:
 
             continue
 
             continue
         line = line.replace( '*', '' )
+
 
         fields = line.split()
+
         # extract rain, year, month
         if fields[5]=="---":
+
         fields = line.split( )
 +
         year = fields[0]
 +
        month = fields[1]
 +
        rain = fields[5]
 +
 
 +
        # it is possible that the rain data is a string
 +
        # of the form "---", in which case taking the float
 +
        # equivalent will result in an exception.  We catch this
 +
        # exception with a try/except clause.
 +
        try:
 +
            tuple = ( float( rain ), month, year )
 +
        except ValueError:
 
             continue
 
             continue
         #print( fields[5] )
+
 
         tuple = ( float(fields[5]), fields[0], fields[1] )
+
         # if we're here, the rain data is correct.  Put it as
         rainDays.append( tuple )
+
         # the first item in a tuple, and record the tuple in a
 +
        # list.
 +
         L.append( tuple )
 +
 
 
          
 
          
     return rainDays
+
     # sort the list and reverse it
 
+
    L.sort()
def main():
+
     L.reverse()
     data = getTemperatureData( "cambridgedata.txt" )
+
      
     #print( data )
 
 
      
 
      
     rainDays = getRainData( data )
+
     # display rainiest month/year
 
+
     rain, month, year = L[0]
     rainDays.sort()
+
     print( "rain = ", rain, "year = ", year, "month = ", month )
    tuple = rainDays[-1]
 
     rain = tuple[0]
 
    year = tuple[1]
 
    month = tuple[2]
 
 
      
 
      
    print( "the rainiest day in Cambridge was in", month, year,
 
          "with", rain, "mm of rain" )
 
 
      
 
      
 
main()
 
main()
 
  
 
</source>
 
</source>

Revision as of 18:15, 30 November 2015

--D. Thiebaut (talk) 10:34, 18 September 2015 (EDT)


Contents

Wed., 9/16/15


# displayWeek.py
# D. Thiebaut
# displays a schedule for the five days of the week

length = eval( input( "Length of bar? " ) )

print( "-" * length )
for day in [ "Mo:", "Tu:", "We:", "Th:", "Fr:" ]:
    print( day )
    #print( "  :" * 2)
    print( "  :" )
    print( "-" * length )


Friday, 9/18/15


# displayGrade.py
# D. Thiebaut
#
# prompts user for information and
# grade.
# display user's grade in graph, along with
# class average (constant)


# constant information
classAvg = 80

# user input.  Get info from user
fName = "Al"
lName = "K"
Id    = "990123456"
final = 90


#--- Format information and display ---
# create the bar 
bar = "+" + 48*"-" + "+"
lenBar = len( bar )

# compute the number of dashes
noSpaces = lenBar - len( fName ) -len(lName )-len(Id) - 6
spaces = " " * noSpaces

# create the scale
scale = "      00...10...20...30...40...50...60...70...80...90...100"

# display the information
print( bar )
print( "|" + fName, lName, spaces, Id, "|" )
print( bar )
print()
print( scale )
# the length of the bar for the bar-graph, in number of characters
# is 1/2 the actual grade.  So divide the grade by half to get the
# number of chars.  We use // to get the integer part of the result
print( "grade:", (final//2) * "#" )
print( "class:", (classAvg//2) * "#" )


Monday, 9/21/15


Version 1 of Teller Machine program

# tellerMachine.py
# D. Thiebaut
# A program that simulates a teller machine, where user enters an amount of
# dollars, and program figures out the number of bills to return.

# get the amount 
amount = eval( input( "Please enter amount: " ) )
amount = int( amount )
print()

# break down into bills
no20s = amount // 20       # how many 20s go into amount
amount = amount % 20    # what is left over after giving out 20s go back into amount

no10s = amount // 10
amount = amount % 10

no5s = amount //5
no1s = amount % 5

# display the results
print( "Amount withdrawn = ", amount )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )


Version 2, submitted by Shirui Cheng, who figured out that it was a perfect opportunity to use a loop!

# Teller machine simulator
# Shirui Cheng

# get the input
amount = eval(input ("How much money do you want to withdraw? "))

# break down in $20-, $10-, $5-, and $1-bills
# and print the result at every step
for bill in (20, 10, 5, 1):
    no = amount // bill
    amount = amount % bill
    print (no, "$", bill, "bill(s)")


Wednesday, 9/23/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about computing
# the average value of a list of numbers.
#
def main():

    # initialize variables
    sum = 0
    count = 0

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "average = ", sum/count )

main()


Accumulating Strings


# stringPatterns.py
# D. Thiebaut
# print a string of 5 alternating patterns.

def main():
    # define 2 different patterns
    pat1 = "**"
    pat2 = "++"

    # create an empty string
    result = ""

    # loop 5 times (we want a string of 5 alternating patterns)
    for i in range( 5 ):
        # add a new pattern to the string
        result = result + pat1

        # switch the patterns around
        pat1, pat2 = pat2, pat1

    # done with the loop!  Print string of patterns
    print( result )

main()


Friday, 9/25/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about debugging a simple
# program and reveal how the loop works.
#
def main():

    # initialize variables
    sum = 0
    count = 0
    print( "sum = {0:1} count = {1:1}".format( sum, count ) )
    input()

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1
        print( "age = {0:5} sum = {1:5} count = {2:5}".format( age, sum, count ) )
        input()

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "sum = ", sum )
    print( "count = ", count )
    print( "average = ", sum/count )

main()



Wed., 9/30/15


# exercises 9/30/15

def main():
    # Exercise #1
    fName = "SOPHIA"
    lName = "smith"

    fullName = fName + " " + lName

    print( fullName.title().center( 60 ) )
    print( "\n", "-"*60, "\n\n", sep="" )
   
    # Exercise #2
    # Define the string with multiple lines
    book = """Ulysses
    James Joyce
    Stately, plump Buck Mulligan came from the stairhead, 
    bearing a bowl of lather on which
    a mirror and a razor lay crossed. 
    """

    # split the book into a list of lines
    lines = book.splitlines()

    # assign different lines to variables
    bookTitle = lines[0]
    author = lines[1]
    sentence = lines[2] + lines[3] + lines[4]
    # this next statement is too sophisticated for now
    #sentence = " ".join( lines[2: ] )

    # display the result
    print( bookTitle.upper().center( 60 ) )
    print( author.title().center( 60 ) )
    print( )
    print( sentence )

main()


Fri., 10/2/15


# Using split()
# -------------------------
poem = """Chocolate
Chocolate is the first luxury.
It has so many things wrapped up in it:
Deliciousness in the moment,
childhood memories,
and that grin-inducing
feeling of getting a reward for being good.
--Mariska Hargitay"""

# display each line centered in 60 spaces.
# first line all uppercase.
# last line right justified in 60 spaces.

lines = poem.split( "\n" )
#print( lines )

# -----------------------------------------------------------------------
# (remove the triple double-quotes to energize the code section)
print( lines[0].upper().center(60) )

for line in lines[1: ]:
    print( line.center( 60 ) )

# -----------------------------------------------------------------------
print( "-" * 60 )

lines[0] = lines[0].upper()
for line in lines[0:3]+lines[4:]:
    print( line.center(60) )


Another Example


# -------------------------
# Another Example
# -------------------------


def printBar():
    print( 60 * '-' )


def sayHello():
    print( )
    print( "Hello, and welcome!" )
    print( )


def main():
    for i in range( 10 ):
        printBar() 
    sayHello()
    printBar()

main()


Happy Birthday


# ------------------------------------------
# Sing Happy Birthday to some Minions
# ------------------------------------------

def singHappyBirthday( minion ):
    print( "Happy birthday to you\n" * 2, end="" )
    print( "Happy birthday, dear", minion )
    print( "Happy birthday to you" )
    print()
    
def main():
    #singHappyBirthday( "Dave" )
    #singHappyBirthday( "Stuart" )
    minions = "Dave, Stuart, Jerry, Jorge, Tim, Mark, Phil, Kevin,  Jon"

    minions = minions.split( ", " )
    #print( minions )
    
    for minion in minions:
        singHappyBirthday( minion.strip() )
        
main()


Mon. 10/5/15


Converting Temperatures


# F  = C * 9 / 5 + 32
def convert( c ):
    return int( c * 9/5 + 32 )


def main():
    for C in [ 0, 32, 100 ] :
        fahr = convert(  C )
        print( fahr )


main()


Transforming dates


def singHappyBirthday( minion ):
    print( "minion = ", minion )
    #print( "Happy birthday to you\n" * 2, end="" )
    #print( "Happy birthday, dear", minion )
    #print( "Happy birthday to you" )
    #print()

def worker0( ):
    return "clap " *  10  

def worker1( num1 ):
    return num1 * 2 + 1 

def worker2( n ):
    return n % 10 

def worker3( p ):
    p = worker1( p )
    p = worker2( p )
    return p 
    
def dateConvert( date ):
    """converts a date in the form "2 Jan 2010" into
    a date of the form 01022010"
    """
    # split the string into day, month, and year
    fields = date.split()
    day = fields[0]
    month = fields[1]
    year = fields[2]

    #print( day, month, year )

    # if the day is less than 10, add a 0 in front of it
    day = ( '0' + day )[-2: ]
    
    #print( day, month, year )

    # find where the month is located in a string of 3-letter
    # months.  If the string is "Jan", then the location will
    # be 0.  If the string is "Feb", then the location will be 3,
    # etc.
    months = "janfebmaraprmayjunjulaugsepoctnovdec"
    index  = months.find( month.lower() )

    # divide by 3 to find the index in the form 0, 1, 2,... 11.
    # and add 1 to make this 1, 2,... 12
    monthDigit = index//3 + 1

    # transform this digit into a string, with a 0 in front
    # in case the digit is less than 10.
    monthDigit = "{0:02}".format( monthDigit )
    
    #print( day, month, year, monthDigit )

    # return the formatted string
    return "{0:1}{1:1}{2:1}".format( monthDigit, day, year )


def main():

    for date in [ "1 Jan 2010", "2 Mar 1900", "31 Dec 2015" ]:
        print( dateConvert( date ) )

    
main()
  • Output:


01012010
03021900
12312015


Fri. 10/9/15


Once More: the Teller Machine program

def getAmount():
    amount = eval( input( "Amount? " ) )
    return abs( amount )

def breakDown( amount ):
    no20s, amount = amount // 20, amount % 20
    no10s, amount = amount // 10, amount % 10
    no5s, amount   = amount // 5, amount % 5
    no1s = amount

    return no20s, no10s, no5s, no1s

def display(orgAmount, No20s, No10s, No5s, No1s ):
    print( orgAmount, No20s, No10s, No5s, No1s )
    return 
    
def main():
    # get amount from user
    amount = getAmount( )
    orgAmount = amount    # keep value of original amount
    
    # get Number of $20-, $10, $5-, and $1-bills
    No20s, No10s, No5s, No1s = breakDown( amount )

    # display result
    display( orgAmount, No20s, No10s, No5s, No1s )

main()


Flipping DNA


def getSentence(  ):
    sentence = input( "Enter a list of words: " )

    words = sentence.split( )
    for i in range( len( words ) ):
        #print( "i = ", i, "words = ", words )
        #print( "words[i] = ", words[i] )
        #print( "words[i].capitalize() = ", words[i].capitalize() )
        words[i] = words[i].capitalize()
        #print( "i = ", i, "words = ", words )
        #print()
        
    return words

def flip( DNA ):
    newDNA = DNA.replace( "A", "t" ).replace( "T", "a" )
    newDNA = newDNA.replace( "G", 'c' ).replace( "C", 'g' )
    return newDNA.upper()
        
def main():

    listWords = getSentence( )
    print( "list = ", listWords )

    print( "AAACGTTTAG", flip( "AAACGTTTAG" ), sep="\n" )
    
main()


Wed 10/14/15


dempolls.raw


<html>
<head>
<title>
Democratic Polls
</title>
</head>
<body>
<h1>Results</h1>
<data />
</body>
</html>


dempolls.data


Biden 19.1
Chafee 0.6
Clinton 44.4
Lessig 0.0
O'Malley 1.0
Sanders 25.1
Webb 1.2


createDynamicWebPage.py


# createDynamicWebPage.py
# D. Thiebaut
# create dynamic Web page


import datetime  # get a library that will allow
                 # us to print today's date.

# getTextFrom(): given a file name, opens the
# file, gets its contents as a string, and return
# it.
def getTextFrom( fileName ):
    file = open( fileName, "r" )
    text = file.read()
    file.close()
    return text

def main():

    # get the raw html file
    html = getTextFrom( "dempolls.raw" )

    # get the raw data file
    candidates = getTextFrom( "dempolls.data" )

    # replace <data /> tag with the data just
    # read in the raw html file.
    today = datetime.datetime.today().strftime("%m/%d/%Y")
    newString = today + "<br />" + candidates
    html = html.replace( "<data />", newString )

    # store new html to file
    file = open( "dempolls.html", "w" )
    file.write( html )
    file.close()

    # tell the user that process is over
    print( "File dempolls.html created, and in your directory!" )
    
main()


Wed. 10/21/15


graphicsDemo.py


# graphicsDemo.py
# D. thiebaut
# A demo program using the graphics library to make a ball
# move on the graphic window.

from graphics import *
from random import *

def main():
    # open the graphic window
    win = GraphWin("My Circle", 600, 600)

    # put some text in the middle of the window
    label = Text( Point( 300, 300 ),
                  "CSC111 Graphics Demo" )
    label.draw( win )
    
    # draw a circle
    x = 50
    y = 50
    r = 20
    c = Circle( Point( x, y ), r )
    red = randint( 0, 255 )
    green = randint( 0, 255 )
    blue = randint( 0, 255 )
    color = color_rgb( red, green, blue )
    c.setFill( color )
    c.draw( win )
    dx = 3
    dy = 0

    for i in range( 1000 ):
        # pick random color
        red = randint( 0, 255 )
        green = randint( 0, 255 )
        blue = randint( 0, 255 )
        color = color_rgb( red, green, blue )
        c.setFill( color )

        # move
        c.move( dx, dy )

        # see if bounce off walls
        center = c.getCenter()
        x = center.getX()
        if x > 600-r or x < 0+r:
            dx = -dx
        #win.getMouse()

        # move label
        label.move( 1, -1 )
        
    # wait for user to click in window
    win.getMouse() 

    # close the window
    win.close()

main()


Mon. 11/2/15


Exception, Version 1


# getInput: returns an integer larger
# than 0.  Expected to be robust…

def getInput():
  while True:
      x = int( input( "Enter a positive int:  " ) )        
      if  x >= 0:
          return x
      print( "invalid input.  Please reenter" )


def main():
   num = getInput()
   print( "you entered", num )

main()


Exception, Version 2


# getInput: returns an integer larger
# than 0.  Expected to be robust…

def getInput():
  while True:
      try:
          x = int( input( "Enter a positive int:  " ) )
      except ValueError:
          print( "Invalid number.  Please reenter" )
          continue
        
      if  x >= 0:
          return x
      print( "invalid input.  Please reenter" )


def main():
   num = getInput()
   print( "you entered", num )

main()


Quadratic Equation with Exceptions


import math

def ZelleExample():
    print( "solution to quadratic equation" )
    try:
        a, b, c = eval( input( "Enter 3 coefficients (a, b, c): " ) )
        disc = math.sqrt( b*b - 4*a*c )
        root1 = (-b + disc )/ (2*a)
        root2 = (-b - disc )/ (2*a)
        print( "solutions: ", root1, roo2 )
    except NameError:
        print( "You didn't enter 3 numbers!" )
    except TypeError:
        print( "Your input contained invalid numbers" )
    except SyntaxError:
        print( "Forgot a comma between the numbers? " )
    except ValueError:
        print( "No real roots, negative discriminant" )
    except:
        print( "Something went wrong..." )

def main():
    ZelleExample()

main()


Dice: Definining Classes


# DiceExample.py
# D. Thiebaut
# our first program where we define a class.
#
# The class implement a die, with some number
# of sides, and a way to "roll" and show a new
# number (value) up top.

# libraries
import random

# a class for a die
class Die:
    def __init__( self, n ):
         self.noSides = n
         self.value      = 1

    def roll( self ):
         self.value = random.randrange( 1, self.noSides+1 )

    def getValue( self ):
         return self.value


def main():
    # Create 2 dice, one with 6 sides, one with 8
    d1 = Die( 6 )
    d2 = Die( 8 )

    # Roll both dice
    d1.roll( )
    d2.roll( )

    # display their value
    print( "Die 1: ",  d1.getValue() )
    print( "Die 2: ",  d2.getValue() )

main()


A program with a class to implement Cat objects


# cats1.py
# D. Thiebaut
# Our first built-from scratch program involving
# classes and objects.
# In this program we read a CSV file containing
# the definitions of cats, one cat per line.
# We extract the name, vaccinated status, breed,
# and age of the cat, and save them in a cat
# object.  the Cat class is used to define how
# a cat object behaves.

class Cat:
    def __init__( self, na, vac, bre, ag ):
        self.name= na
        self.vaccinated = vac
        self.breed = bre
        self.age = ag

    def isVaccinated( self ):
        #if self.vaccinated == True:
        #    return True
        #else:
        #    return False
        return self.vaccinated

    def getName( self ):
        return self.name
    
def main():
    # Minou, 3, vac, stray
    text = """Minou, 3, vac, stray
    Max, 1, not-vac, Burmese
    Gizmo, 2, vac, Bengal
    Garfield, 4, not-vac, Orange Tabby"""

    catList = [] 
    for line in text.split( "\n" ):
        words = line.split( "," )
        if words[2].find( "not" ) != -1:
            vac = False
        else:
            vac = True
        age = int( words[1].strip() )
            
        cat = Cat( words[0], vac, words[3], age )
        catList.append( cat )
        
    #cat1 = Cat( "Minou", True, "stray", 3 )
    for cat in catList:    
        if cat.isVaccinated():
            print( cat.name, "is vaccinated" )
        else:
            print( cat.getName(), "is not vaccinated" )

main()


Cats1.csv File


Max, 1, vaccinated, not tattooed, stray
Black, 3, not vaccinated, tattooed, stray
Gizmo, 2, vaccinated, not tattooed, Bengal
Winston, 2, vaccinated, tattooed, Bengal
Garfield, 3, vaccinated, not tattooed, Burmese
Bob, 4, not vaccinated, tattooed, Orange Tabby
Minou, 1, vaccinated, tattooed, stray
Silky, 3, not vaccinated, tattooed, Siamese


Wed. 11/04/15


cats1.py


# cats1.py
# D. Thiebaut
# Our first built-from scratch program involving
# classes and objects.
# In this program we read a CSV file containing
# the definitions of cats, one cat per line.
# We extract the name, vaccinated status, breed,
# and age of the cat, and save them in a cat
# object.  the Cat class is used to define how
# a cat object behaves.

class Cat:
    def __init__( self, na, vac, bre, ag ):
        self.name= na
        self.vaccinated = vac
        self.breed = bre
        self.age = ag

    def isVaccinated( self ):
        #if self.vaccinated == True:
        #    return True
        #else:
        #    return False
        return self.vaccinated

    def getBreed( self ):
        return self.breed
    
    def getName( self ):
        return self.name

    def __str__( self ):
        if self.vaccinated == True:
            vacc = "vaccinated"
        else:
            vacc = "not vaccinated"
        return "{0:1}: {1:1} {2:1} {3:1} yrs old".format(
            self.name, vacc, self.breed, self.age )
    

def getCatList( fileName ):
    """parses a collection of lines and creates
    a list of cat objects."""

    file = open( fileName, "r" )
    text = file.read()
    file.close()

    catList = [] 
    for line in text.split( "\n" ):
        words = line.split( "," )
        
        if len( words ) != 4:
            continue
        
        if words[2].find( "not" ) != -1:
            vac = False
        else:
            vac = True
        age = int( words[1].strip() )
            
        cat = Cat( words[0].strip(), vac, words[3].strip(), age )
        catList.append( cat )
        
    return catList

def main():
    fileName = input( "File name? " )
    catList = getCatList( fileName )
    for cat in catList: print( cat )

    for cat in catList:
        if not cat.isVaccinated() and cat.getBreed()=="stray":
            print( cat )
        
    return
main()


cats2.csv


Minou, 3, vac, stray
Max, 1, not-vac, Burmese
Gizmo, 2, vac, Bengal
Garfield, 4, not-vac, Orange Tabby
Smudges, 10, not-vac, stray
Minique, 5, vac, French Tabby



Fri. 11/06/15


Wheel1.py


The beginning of a car...

# Graphics with Objects.
# Demo program illustrating how we are going to
# build a car.
# 
from graphics import *

#define global geometry of window
WIDTH = 800
HEIGHT = 600


def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Wheel Demo", WIDTH, HEIGHT )

    c1 = Circle( Point( WIDTH//2, HEIGHT//2 ), 30 )
    c1.draw( win )


    win.getMouse()
    win.close()

main()


Car1.py


# Graphics with Objects.
# Demo program illustrating how we are going to
# build a car.
# 
from graphics import *

#define global geometry of window
WIDTH = 800
HEIGHT = 600

class Wheel:
    def __init__( self, center, radius ):
        self.c1 = Circle( center, radius )
        self.c2 = Circle( center, radius//2 )
        self.c1.setFill( "black" )
        self.c2.setFill( "white" )

    def draw( self, win ):
        self.c1.draw( win )
        self.c2.draw( win )

    def move( self, dx, dy ):
        self.c1.move( dx, dy )
        self.c2.move( dx, dy )

    

class Car:
    def __init__( self, refPoint ):
        x1 = refPoint.getX()
        y1 = refPoint.getY()
        x2 = x1 + 180
        y2 = y1 + 60
        self.body = Rectangle( Point( x1 , y1 ), Point( x2 , y2 ) )
        self.w1 = Wheel( Point( x1+40, y1+60 ), 20 )
        self.w2 = Wheel( Point( x1+140, y1+60 ), 20 )
        self.body.setFill( "yellow" )

    def draw( self, win ):
        self.body.draw( win )
        self.w1.draw( win )
        self.w2.draw( win )

    def move( self, dx, dy ):
        self.body.move( dx, dy )
        self.w1.move( dx, dy )
        self.w2.move( dx, dy )
                  
        
def main():
    """Open a graphic window and puts graphic object(s)
    on it.  Wait for user to click mouse to close.
    """
    global WIDTH, HEIGHT

    # open window
    win = GraphWin( "Wheel Demo", WIDTH, HEIGHT )

    # Create a list of 10 cars
    cars = [] # empty list
    for i in range( 10 ):
        # create a new car
        c1 = Car( Point( i*10, i*90 ) )

        # draw the car just created
        c1.draw( win )
        
        # add car just created to the list
        cars.append( c1 ) 

    dx = 3 # x-direction for movement of cars
    dy = 0 # y-direction for movement of cars

    # animation loop
    while True:
        # move all the cars in the list
        for c1 in cars:
            c1.move( dx, dy )

        # if user clicks the mouse, change the
        # direction of movement.
        if win.checkMouse() != None:
            dx = -dx
        
    # wait for user to click the mouse to close window
    win.getMouse()
    win.close()

main()



Mon. 11/9/15


Images to Play With


catGlasses.gif


CatGlasses.gif


imageProcessing.py


# imageProcessingSkel.py
# D. Thiebaut
# A skeleton program to start doing image processing
# in Python
from graphics import *
# image geometry = 424x18
# make the window the same geometry

WIDTH  = 424
HEIGHT = 418

def main():
    # open the window
    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )

    # get the file name from the user
    fileName = "catGlasses.gif" #input( "Image name? " )
    print()
    
    # open the cat image
    cat = Image( Point(WIDTH//2, HEIGHT//2), fileName )
    cat.draw( win )

    # wait for user to click the mouse...
    win.getMouse()

    # and close the window
    win.close()

main()


pixelProcessing.py


# pixelProcessingSkel.py
# D. Thiebaut
# Simple skeleton program for processing
# the pixels of an image

from graphics import *

WIDTH  = 424   
HEIGHT = 418   
IMAGEFILENAME = "catGlasses.gif" 


    
def makeRed( img, win ):
    """ set the red component of all the pixels to 255, the
    maximum value.
    """
    global WIDTH, HEIGHT

    for y in range( HEIGHT ):
        win.update()  # refresh the window after each inner loop
        for x in range( WIDTH ):
            red, green, blue = img.getPixel( x, y )
            green, blue, red = red, green, blue
            red = 255
            img.setPixel( x, y, color_rgb(red, green, blue ) )


def main():

    win = GraphWin( "CSC111 Image Viewer", WIDTH, HEIGHT )

    img = Image( Point(WIDTH//2, HEIGHT//2), IMAGEFILENAME )
    img.draw( win )

    makeRed( img, win )
    
    win.getMouse()
    win.close()

main()


Wed. 11/11/15


chessboard0.py


# chessboard0,py
# D. Thiebaut
# Displays an 8x8 chessboard.

from graphics import *

WIDTH = 600
HEIGHT = 600

            
def displayBoard( win ):
    """display an 8x8 chessboard in the square
    window defined by WIDTH and HEIGHT"""
    global WIDTH, HEIGHT

    # compute size of square 
    side = WIDTH//8

    # display 8 columns and 8 rows of squares
    for i in range( 0, 8 ):
        for j in range( 0, 8 ):

            # create a rectangle
            r = Rectangle( Point( i*side, j*side ),
                           Point( i*side+side, j*side+side ) )

            # figure out if it should be black or white
            if (i+j) % 2 != 0:
                r.setFill( "grey" )
            else:
                r.setFill( "white" )

            # draw it
            r.draw( win )

def main():
    global WIDTH, HEIGHT

    # open a graphics window.
    win = GraphWin( "Chessboard", WIDTH, HEIGHT )

    # display the board.
    displayBoard( win )

    # wait for use to click window, and close up.       
    win.getMouse()
    win.close()

main()


Gifs for the game of Checkers

  • piece.gif

Piece.gif

  • piece2.gif

Piece2.gif


Interactive Checkers

InteractiveCheckers.png


# Interactive game of checkers.
# This program creates an 8x8 board, with
# alternating white and dark cells.  It then positions
# 3 rows of white pieces, and 3 rows of black pieces at
# the top and bottom of the board, respectively.
# It then allows the user to click on various pieces to make them 
# disappear.

from graphics import *

WIDTH = 600
HEIGHT = 600

class CheckersPiece:
    """ a class defining a checkers piece.  It uses gif images for
    each piece."""

    def __init__(self, color, i, j ):
        global WIDTH, HEIGHT
        side = WIDTH // 8
        x = i * side
        y = j * side
        centerPoint = Point( x+side//2, y+side//2 )
        if color=="white":
            self.img = Image( centerPoint, "piece2.gif" )
        else:
            self.img = Image( centerPoint, "piece.gif" )
            
    def draw( self, win ):
        self.img.draw( win )
        
    def undraw( self ):
        self.img.undraw()

    def getCoords( self ):
        global WIDTH, HEIGHT
        side = WIDTH // 8
        x = self.img.getAnchor().getX()
        y = self.img.getAnchor().getY()
        return x-side//2, y-side//2, x+side//2, y+side//2

def isWhite( i, j ):
    if (i+j)%2==0:
        return True
    return False
           
def displayBoard( win ):
    """this function displays the 8x8 board, with alternating
    colors."""
    global WIDTH, HEIGHT
    side = WIDTH//8
    for i in range( 0, 8 ):
        for j in range( 0, 8 ):
            r = Rectangle( Point( i*side, j*side ),
                           Point( i*side+side, j*side+side ) )
            if not isWhite( i, j ):
                r.setFill( "grey" )
            else:
                r.setFill( "white" )
            r.draw( win )

def createDisplayPieces( win ):
    """create 3 rows of white pieces at the top,
    and 3 rows of black pieces at the bottom.  Put them
    only on dark squares."""
    whites = []
    blacks = []
    for i in range( 8 ):
        for j in range( 3 ):
            if isWhite( i, j ):
                continue
            c = CheckersPiece( "white", i, j )
            c.draw( win )
            whites.append( c )
            
        for j in range( 5, 8 ):
            if isWhite( i, j ):
                continue
            c = CheckersPiece( "black", i, j )
            c.draw( win )
            blacks.append( c )

    return whites, blacks

def findClickedPiece( p, whites, blacks ):
    """loop through all the pieces and see if the point that the user
    clicked (p) is inside the square that corresponds to where the piece
    is."""
    for c in whites + blacks:
        x1, y1, x2, y2 = c.getCoords()
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            return c
    return None

def main():
    global WIDTH, HEIGHT
    
    win = GraphWin( "Chessboard", WIDTH, HEIGHT )

    displayBoard( win )
    whites, blacks = createDisplayPieces( win )

    # animation loop
    while True:
        p = win.getMouse()
        c = findClickedPiece( p, whites, blacks )
        if c != None:
            c.undraw( )
            
            
            
    win.getMouse()
    win.close()

main()


Mon, 11/16/15


Button Demo


# buttonDemo.py
# D. Thiebaut

from graphics import *
WIDTH = 600
HEIGHT = 600

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    button1 = Button( 10, 10, 60, 30, "OK" )
    button1.draw( win )
    
    while True:
        clickPoint = win.getMouse()
        if button1.isClicked( clickPoint ):
            print( "button clicked!" )

    win.close()

main()


Random Circles Demo


# randomDotDemo.py
# D. Thiebaut

from graphics import *
from random import *

WIDTH = 600
HEIGHT = 600

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def undraw( self ):
        self.frame.undraw( )
        self.label.undraw( )
        
    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def addDot( win ):
    x = randint( 100, 500 )
    y = randint( 100, 500 )
    center = Point( x, y )
    radius = randint( 10, 600 )
    c = Circle( center, radius )
    r = randint( 0, 255 )
    g = randint( 0, 255 )
    b = randint( 0, 255 )
    c.setFill( color_rgb( r, g, b ) )
    c.draw( win )
    
def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    # create UI
    OkButton = Button( 10, 10, 60, 30, "Ok" )
    OkButton.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )
    addDotButton = Button( 150, 10, 60, 30, "Add Dot" )
    addDotButton.draw( win )
    
    # animation loop
    while True:
        clickedPoint = win.getMouse()
        if quitButton.isClicked( clickedPoint ):
            break
        
        if OkButton.isClicked( clickedPoint ):
            print( "button clicked!" )
            continue

        if addDotButton.isClicked( clickedPoint ):
            addDot( win )

            # redraw the buttons, just in case a circle covers them.
            OkButton.undraw()
            OkButton.draw( win )
            quitButton.undraw()
            quitButton.draw( win )
            addDotButton.undraw()
            addDotButton.draw( win )

            # go back to top of loop and wait for another click from user...
            continue
        
        # if we're here, it's because user
        # clicked on window, but not on buttons
        print( "clicked on window" )
        
    win.close()

main()


Polygon Demo


# buttonDemo.py
# polygon demo.  A triangle
# and a start are drawn
# D. Thiebaut

from graphics import *
from random import *
WIDTH = 300
HEIGHT = 300


def main():
    global WIDTH, HEIGHT
    win = GraphWin( "Pushbutton Demo", WIDTH, HEIGHT )

    # draw a triangle
    points = [ Point( 100, 100 ),
               Point( 150, 100 ),
               Point( 175, 150 ) ]
    poly1 = Polygon( points )
    poly1.setFill( "orange" )
    poly1.draw( win )

    # draw a star
    points = [ Point( 35, 120 ),
               Point( 37.9,129.1),
               Point( 46.9,129.1),
               Point( 39.7,134.5),
               Point( 42.3, 143.1 ),
               Point( 35, 139 ),
               Point( 27.7, 143.1 ),
               Point( 30.3, 134.5 ),
               Point( 23.1, 129.1 ),
               Point( 32.1, 129.1 ) ]
    poly2 = Polygon( points )
    poly2.setFill( "lightgreen" )
    poly2.draw( win )
    
    # wait for user to click mouse to close window
    win.getMouse()
    win.close()

main()


Create Polygon With Mouse Demo: Skeleton Program


SmithLogo.gif
# recordShapeIntoPolygon.py
# D. Thiebaut
# Brings up a gif image in the window
# and creates 2 buttons.
# User can click on buttons to 1) start
# Recording a shape, and 2) stop recording
# it and displaying it as light-blue polygon.
# WARNING: the program, as is, is not safe and will crash
#   if the user start the program and clicks on a point that is not
#   inside a button...   The problem is with pointList...

from graphics import *

WIDTH = 400
HEIGHT = 400

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def main():
    global WIDTH, HEIGHT

    # create a graphic window
    win = GraphWin( "Record Polygon", WIDTH, HEIGHT )

    # put a logo of Smith in the middle of the window
    img = Image( Point(WIDTH//2, HEIGHT//2), "smithLogo.gif" )
    img.draw( win )

    # create 2 buttons
    startB = Button( 10, 10, 60, 30, "Start" )
    startB.draw( win )
    stopB = Button( 80, 10, 60, 30, "Stop" )
    stopB.draw( win )

 # animation loop
    while True:
        clickedPoint = win.getMouse()
        
        # quit?
        if exitB.isClicked( clickedPoint ):
            break

        # start recording polygon?
        if startB.isClicked( clickedPoint ):
            pointList = []
            continue

        # stop recording polygon?
        if stopB.isClicked( clickedPoint):
            poly = Polygon( pointList )
            poly.setFill( "cyan" )
            poly.draw( win )
            continue

        # if we're here, it's because
        # the user clicked somewhere outside
        # the buttons
        pointList.append( clickedPoint )

    win.getMouse()   
    win.close()

main()



Wed. 11/18/15


SavePolyToFile.py


# recordShapeIntoPolygon.py
# D. Thiebaut
# Brings up a gif image in the window
# and creates 2 buttons.
# User can click on buttons to 1) start
# Recording a shape, and 2) stop recording
# it and displaying it as light-blue polygon.

from graphics import *

WIDTH = 400
HEIGHT = 400
CSVFILE = "map.csv"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            if self.clicked:
                self.frame.setFill( "yellow" )
            else:
                self.frame.setFill( "white" )
            return True
        else:
            return False

def savePolyToFile( pointList, fileName ):
    file = open( fileName, "a" )

    L = []
    for point in pointList:
        x = point.getX()
        y = point.getY()
        #print( x, y )
        L.append( str( x ) ) # "{0:1}".format( x )
        L.append( str( y ) )
        
    #print( ", ".join( L ) )
    file.write( ", ".join( L )  )
    file.close()
    
def main():
    global WIDTH, HEIGHT, CSVFILE

    # create a graphic window
    win = GraphWin( "Record Polygon", WIDTH, HEIGHT )

    # put a logo of Smith in the middle of the window
    img = Image( Point(WIDTH//2, HEIGHT//2), "smithLogo.gif" )
    img.draw( win )

    # create 2 buttons
    startB = Button( 10, 10, 60, 30, "Start" )
    startB.draw( win )
    stopB = Button( 80, 10, 60, 30, "Stop" )
    stopB.draw( win )
    exitB = Button( 150, 10, 60, 30, "Quit" )
    exitB.draw( win )

    # create empty list of points
    pointList = []
    
    # animation loop
    while True:
        clickedPoint = win.getMouse()
        
        # quit?
        if exitB.isClicked( clickedPoint ):
            break

        # start recording polygon?
        if startB.isClicked( clickedPoint ):
            pointList = []
            continue

        # stop recording polygon?
        if stopB.isClicked( clickedPoint):
            poly = Polygon( pointList )
            poly.setFill( "cyan" )
            poly.draw( win )
            savePolyToFile( pointList, CSVFILE ) 
            continue

        # if we're here, it's because
        # the user clicked somewhere outside
        # the buttons
        pointList.append( clickedPoint )

        
    win.getMouse()        
    win.close()

main()


Different Button Class


class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked  # toggle
            
            #if self.clicked:
            #    self.frame.setFill( "yellow" )
            #else:
            #    self.frame.setFill( "white" )
            
            self.frame.setFill( "red" )
            sleep( 0.1 ) # 0.1 sec
            self.frame.setFill( "white" )
            
            return True
        else:
            return False


Fri. 11/20/15


Merge 2 Lists


# merging.py
# D. Thiebaut
# given 2 lists (in the form of stings), one a list
# of Disney characters, one a fictitious list of
# characters and their phone number, merge the
# two lists and print the name of the Disney characters
# and the area code they live in.

# master list
master = """Mickey Mouse, Mickey Mouse Clubhouse
Pluto, The Chain Gang
Minnie Mouse, Plane Crazy
Donald Duck, The Wise Little Hen
bambi, bambi
Pluto The Chain Gang
"""

# secondary list
phones = """Sue Green, (413) 585 1122, single
Alex Marquis, (617) 512 1234, married
Bambi, single
Mickey Mouse, (212) 123 5598, married
Pluto, (212) 123 5598, single
Minnie Mouse, (212) 123 5598, married
Donald Duck, (617) 525 1212, single
Bambi, (209) 713 1139, single
"""

def findPhone( name ):
    """given name, a stripped name of a Disney
    character, locate line in phone list, and
    return line that contains phone associated
    with name.
    """
    # split the string into lines
    phoneList = phones.split( "\n" )

    # look at each line and see if we find the
    # character with the given name
    for line in phoneList:
        
        # is it valid?  Does it contain commas?
        if len( line.split( "," ) ) != 3:
            # no
            continue

        # if we're here, we know the phone line is valid
        # make it lower case, and see if our name is included in it
        if line.lower().find( name ) != -1:

            # look for the position of the left parenthesis
            index = line.find( "(" )

            # return a slice of the string, containing the 3 area code digits
            return line[index+1:index+4]

    # if we didn't find the name in any of the lines,
    # return None, indicating "not found"
    return None    

def main():

    # split the master list into lines
    disneyList = master.split( "\n" )

    # for each line in the master list...
    for character in disneyList:

        # clean up the line
        character = character.strip()

        # if not a blank line, process it
        if len( character ) > 1:
            
            # get rid of lines not containing commas
            fields = character.split(",")
            if len( fields ) <= 1:
                continue

            # get the name
            name = fields[0].lower().strip()

            # look in secondary list, and fine
            # the line associated with name
            phoneLine = findPhone( name )

            # if we found a phone number, list it, otherwise
            # skip that Disney character...
            if phoneLine != None:
                print( name.title(), phoneLine )
            

main()


ColorOf


def colorOf( year ):
	# assume year is an int
	if year <= 1799:
		return 215, 97, 1
	# if we're here... year is
	# above 1800  
	elif year <= 1899:
		return 241, 163, 64
	elif year <= 1999:
		return 253, 184, 99
	else:
		return 254, 224, 144


Aquarium Skeleton Program

# Aquarium.py
# D. Thiebaut
# A program with fish swimming in an aquarium
# Two buttons control the creation of new fish, and how to
# quit the program.

from graphics import *
from time import sleep
from random import *

# Global variables
WIDTH = 700
HEIGHT = 517
AQUARIUM = "Tank2.gif"
FISH = "Fish10.gif"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            self.frame.setFill( "red" )
            sleep(0.1)
            self.frame.setFill( "white" )
            return True
        else:
            return False

class Fish:
    """A class for a fish, which is an image
    """
    def __init__( self, p ):
        self.img = Image( p, FISH )

    def draw( self, win ):
        self.img.draw( win )

    def autoMove( self ):
        """makes the fish move. If the fish disappears from
        the right, it automatically reappears on the left."""
        dy = randint( -3, 3 )  # random vertical drift
        dx = randint( 0, 3 )   # random move forward
        self.img.move( dx, dy )

        # has it disappeared on the right?
        x = self.img.getAnchor().getX()
        if x > WIDTH + 50:
            # yes, move it to the left of the aquarium
            self.img.move( -WIDTH-50, 0 )

    
#---------------------------------------------------------------
# Functions
#---------------------------------------------------------------

def main():
    """The main interactive function.  Declare the graphic
    window, populate it with a map and 2 buttons, and allows
    the user to enter different polygons."""
    global WIDTH, HEIGHT, AQUARIUM

    # create graphic window.  make it the size of the map.
    win = GraphWin( "Aquarium", WIDTH, HEIGHT )

    # display background image
    map = Image( Point( WIDTH//2, HEIGHT//2 ), AQUARIUM )
    map.draw( win )

    # create 2 buttons to start recording and record points to
    # csv
    addFish = Button( 10, 10, 60, 30, "Add Fish" )
    addFish.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )

    fishList = []
    
    # animation loop
    while True:
        # has user clicked on a point?
        clickedPoint = win.checkMouse()

        # yes, mouse clicked.
        if clickedPoint != None and quitButton.isClicked( clickedPoint ):
            break
        
        # was it on add-fish button?
        if clickedPoint != None and addFish.isClicked( clickedPoint ):
            clickedPoint = win.getMouse()
            fish = Fish( clickedPoint )
            fish.draw( win )
            fishList.append( fish )
            continue

        for fish in fishList:
            fish.autoMove()

        
    # We currently will not reach this point, as there is
    # no way to escape the while loop.  A good idear would be
    # to add an exit button.
    win.close()

main()



Aquarium.py (Fully functional)


# Aquarium.py
# D. Thiebaut
# A program with fish swimming in an aquarium
# Two buttons control the creation of new fish, and how to
# quit the program.

from graphics import *
from time import sleep
from random import *

# Global variables
WIDTH = 700
HEIGHT = 517
AQUARIUM = "Tank.gif"
FISH = "Fish10.gif"

class Button:
    """Implements a button, which contains a label (text),
    is rectangular (frame), and can be clicked (True) or not clicked."""
    
    def __init__(self, x1, y1, w, h, text ):
        """constructor: pass (x,y) of top left corner,
        and width and height.  Pass the text displayed in
        button."""
        p1 = Point( x1, y1 )
        p2 = Point( x1+w, y1+h )
        self.frame = Rectangle( p1, p2 )
        self.frame.setFill( "white" )
        self.label = Text(Point( x1+w//2, y1+h//2 ), text )
        self.clicked = False
        
    def draw( self, win ):
        """display button on window."""
        self.frame.draw( win )
        self.label.draw( win )

    def isClicked( self, p ):
        """Checks if p is inside the frame of the button.  Returns True
        if p inside frame, False otherwise.  If p inside, button
        changes state and color."""
        x1, y1 = self.frame.getP1().getX(), self.frame.getP1().getY()
        x2, y2 = self.frame.getP2().getX(), self.frame.getP2().getY()
        
        if x1 <= p.getX() <= x2 and y1 <= p.getY() <= y2:
            self.clicked = not self.clicked
            self.frame.setFill( "red" )
            sleep(0.1)
            self.frame.setFill( "white" )
            return True
        else:
            return False

class Fish:
    """A class for a fish, which is an image
    """
    def __init__( self, p ):
        self.img = Image( p, FISH )

    def draw( self, win ):
        self.img.draw( win )

    def autoMove( self ):
        """makes the fish move. If the fish disappears from
        the right, it automatically reappears on the left."""
        dy = randint( -3, 3 )  # random vertical drift
        dx = randint( 0, 3 )   # random move forward
        self.img.move( dx, dy )

        # has it disappeared on the right?
        x = self.img.getAnchor().getX()
        if x > WIDTH + 50:
            # yes, move it to the left of the aquarium
            self.img.move( -WIDTH-50, 0 )

    
#---------------------------------------------------------------
# Functions
#---------------------------------------------------------------

def main():
    """The main interactive function.  Declare the graphic
    window, populate it with a map and 2 buttons, and allows
    the user to enter different polygons."""
    global WIDTH, HEIGHT, AQUARIUM

    # create graphic window.  make it the size of the map.
    win = GraphWin( "Aquarium", WIDTH, HEIGHT )

    # display background image
    map = Image( Point( WIDTH//2, HEIGHT//2 ), AQUARIUM )
    map.draw( win )

    # create 2 buttons to start recording and record points to
    # csv
    addFish = Button( 10, 10, 60, 30, "Add Fish" )
    addFish.draw( win )
    quitButton = Button( 80, 10, 60, 30, "Quit" )
    quitButton.draw( win )

    fishList = []
    
    # animation loop
    while True:
        # has user clicked on a point?
        clickedPoint = win.checkMouse()

        # yes, mouse clicked.
        if clickedPoint != None and quitButton.isClicked( clickedPoint ):
            break
        
        # was it on add-fish button?
        if clickedPoint != None and addFish.isClicked( clickedPoint ):
            clickedPoint = win.getMouse()
            fish = Fish( clickedPoint )
            fish.draw( win )
            fishList.append( fish )
            continue

        for fish in fishList:
            fish.autoMove()

        
    # We currently will not reach this point, as there is
    # no way to escape the while loop.  A good idear would be
    # to add an exit button.
    win.close()

main()


Mon. 11/23/15


Milou.gif

CountUniqueDogsWithLists.py


# countingUniqueDogsWithList.py
# D. Thiebaut
# Use a list to figure out how many unique
# dogs are in the text variable.

# a text string with many dogs.  Some are repeats.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

# MAIN:
def main():
    dogsFound = []
    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]

        # have we seen it yet?
        if not name in dogsFound:
            dogsFound.append( name )


    print( "We have found", len( dogsFound ), "dog(s)" )
    print( "The dogs found are", ", ".join( dogsFound ) )

main()


CountingDogsWithDictionary.py


# countingUniqueDogsWithDictionary.py
# D. Thiebaut
# Demonstration program of the use of a dictionary
# to keep track of unique elements found in a collection
# of lines.

# the list of dogs, and some value associated with each one.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

def main():

    # create an empty dictionary
    dogsFound = {}

    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]
        age = int( fields[1] )

        # have we seen it yet?
        dogsFound[ name ] = age

    print( "We have found", len( dogsFound ), "dog(s)" )
    print( "The dogs found are:" )

    for name in dogsFound.keys():
        print( name, dogsFound[ name ] )

main()


Keeping track of the first, last, and dogs with largest key found


# countingUniqueDogsWithDictionary.py
# D. Thiebaut
# Demonstration program of the use of a dictionary
# to keep track of unique elements found in a collection
# of lines.

# the list of dogs, and some value associated with each one.
text = """Fido 0
Fido 10
Rex 0
Milou 9
Ralph 3
Fido 8
"""

def main():
    dogsFoundFirst = {}
    dogsFoundLast = {}
    dogsFoundLargest = {}
    
    # look at each line of text
    for line in text.split( "\n" ):

        # split the line on white space
        fields = line.capitalize().strip().split()

        # if invalid, skip
        if len( fields ) != 2:
            continue

        # get dog name
        name = fields[0]
        age = int( fields[1] )

        # Keep the first dog we see
        if not name in dogsFoundFirst.keys():    
            dogsFoundFirst[ name ] = age

        # Keep the last dog we see
        dogsFoundLast[ name ] = age

        # Keep the dog with the largest associated value
        if not name in dogsFoundLargest.keys():
            dogsFoundLargest[ name ] = age
        else:
            # is the new dog found with an age larger than
            # what we have recorded so far, for this dog?
            if dogsFoundLargest[ name ] < age:
                # no, update and keep the new larger age
                dogsFoundLargest[ name ] = age

    print( "We have found", len( dogsFoundFirst ), "unique dog(s)" )
    
    print( "The first dogs found are:" )
    for name in dogsFoundFirst.keys():
        print( " -", name, dogsFoundFirst[ name ] )


    print( "\nThe last dogs found are:" )
    for name in dogsFoundLast.keys():
        print( " -", name, dogsFoundLast[ name ] )

    print( "\nThe dogs with the largest age found are:" )
    for name in dogsFoundLargest.keys():
        print( " -", name, dogsFoundLargest[ name ] )

main()


Nov. 30, 2015


Cambridge.data



Email Addresses


  • These are fake addresses of students registered for different classes. The goal is to create a list of unique addresses for Smith students present in the list, as well as unique addresses for 5-College students.


Ammie@hampshire.edu
Bessie@smith.edu
Carylon@smith.edu
Cheryll@smith.edu
Cordelia@smith.edu
Illa@smith.edu
Lisbeth@smith.edu
Mackenzie@smith.edu
Maryellen@smith.edu
Matha@smith.edu
Patrica@hampshire.edu
Sanjuana@smith.edu
Sharie@smith.edu
Sonya@smith.edu
Yuko@smith.edu

Cheryll@smith.edu
Codi@smith.edu
Cordelia@smith.edu
Elenore@smith.edu
Emelia@smith.edu
Josie@smith.edu
Lina@smith.edu
Lisbeth@smith.edu
Mackenzie@smith.edu
Margaretta@smith.edu
Rosalina@smith.edu
Sade@smith.edu
Sanjuana@smith.edu
Shelli@smith.edu
Sonya@smith.edu

Aja@hampshire.edu
Alesia@hampshire.edu
Bessie@smith.edu
Carylon@smith.edu
Chun@smith.edu
Codi@smith.edu
Constance@smith.edu
Felisa@smith.edu
Illa@smith.edu
Karena@hampshire.edu
Kortney@hampshire.edu
Lina@smith.edu
Mallie@smith.edu
Patrica@hampshire.edu
Zofia@smith.edu

Anna@hampshire.edu
Bessie@smith.edu
Candyce@smith.edu
Clorinda@smith.edu
Elenore@smith.edu
Karine@hampshire.edu
Kaylee@hampshire.edu
Kortney@hampshire.edu
Lisbeth@smith.edu
Marline@smith.edu
Matha@smith.edu
Porsha@hampshire.edu
Renata@smith.edu
Sanjuana@smith.edu
Sonya@smith.edu

Ammie@hampshire.edu
Anna@hampshire.edu
Birdie@smith.edu
Codi@smith.edu
Constance@smith.edu
Edelmira@smith.edu
Kaylee@hampshire.edu
Kristel@hampshire.edu
Margaretta@smith.edu
Moira@smith.edu
Patrica@hampshire.edu
Phuong@hampshire.edu
Sade@smith.edu
Tamela@smith.edu
Yuko@smith.edu


Presidents.txt


  • The content of the file with all the presidents, up to G. W. Bush, can be found here.



rainyCambridge.py


# rainyCambridge.py
# D. Thiebaut
# This program finds the rainiest month and year
# in Cambridge, UK.   The file is fetched from a
# Web site (http://cs.smith.edu/~dthiebaut/UKTemperatures/)
# where several text files are stored, including one
# for Cambridge, called cambridgedata.txt.
# Information on how to retrieve URLs found at
# https://docs.python.org/3.0/library/urllib.request.html
# and http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string

i

import urllib.request   # library for accessing Web pages

#--- some constants used by the program ---
BASEURL = "http://cs.smith.edu/~dthiebaut/UKTemperatures/"


def getTemperatureData( fileName ):
    # fetches the file from the Website and returns it as
    # a long string.
    # format of the file, with a sample of the lines:
    """Cambridge NIAB
Location: 5435E 2606N, 26 metres amsl
Estimated data is marked with a * after the value.
Missing data (more than 2 days missing in month) is marked by  ---.
Sunshine data taken from an automatic Kipp & Zonen sensor marked with a #, otherwise sunshine data taken from a Campbell Stokes recorder.
   yyyy  mm   tmax    tmin      af    rain     sun
              degC    degC    days      mm   hours
   1959   1    4.4    -1.4      20     ---    78.1
   1961   1    6.2     1.0       5    55.9    50.5
   1961   2   10.3     4.3       0    50.4    65.9
   2011   7   21.2    10.9       0    38.4*    ---
   2015   1    8.0     1.3      12    48.4*   ---    Provisional


    """
    url = BASEURL + fileName
    print( "Retrieving data from", url )
    f = urllib.request.urlopen( url )
    data = f.read().decode("utf-8") 
    return data

def main():
    # get the string
    data = getTemperatureData( "cambridgedata.txt" )

    # split string into lines
    lines = data.split( "\n" )
    
   
    # create empty list
    L = []
    
    # loop through all the lines
    for line in lines:

        # strip extra space and remove the * characters
        line = line.strip().replace( '*', ' ' )
        
        # get rid of blank lines
        if len( line ) <= 1:
            continue

        # get preamble
        if not line[0] in ['1', '2']:
            continue

        # extract rain, year, month
        fields = line.split( )
        year = fields[0]
        month = fields[1]
        rain = fields[5]

        # it is possible that the rain data is a string
        # of the form "---", in which case taking the float
        # equivalent will result in an exception.  We catch this
        # exception with a try/except clause.
        try:
            tuple = ( float( rain ), month, year )
        except ValueError:
            continue

        # if we're here, the rain data is correct.  Put it as
        # the first item in a tuple, and record the tuple in a
        # list.
        L.append( tuple )

        
    # sort the list and reverse it
    L.sort()
    L.reverse()
    
    
    # display rainiest month/year
    rain, month, year = L[0]
    print( "rain = ", rain, "year = ", year, "month = ", month )
    
    
main()


processEmails.py


# processEmail.py
# D. Thiebaut
# Takes a list containing several (fake) email addresses and
# splits them into two lists of unique addresses, one for Smith
# and one for 5-college students.

emails = """Ammie@hampshire.edu
Bessie@smith.edu

Carylon@smith.edu
Bessie@smith.edu

Ammie@hampshire.edu
"""

def main():
    # create an empty list for Smith, and
    # an empty list for 5-col
    smith = []
    fiveCol = []

    # process each line and add to appropriate
    # list.
    for line in emails.split( "\n" ):
        line = line.strip()
        if len( line ) <= 1:
            continue
        if line.find( "@smith" ) != -1:
            if not line in smith:
                smith.append( line )
        else:
            if not line in fiveCol:
                fiveCol.append( line )

    print( "Smith = ", smith )
    print( "5-col = ", fiveCol )


main()


Presidents.txt


# presidents.py
# D. Thiebaut

text = """
 copy paste the list of presidents here...
"""

def main():
    # split into individual lines
    lines = text.split( "\n" )

    # display header
    print( lines[0] )

    # process all but first line
    for line in lines[1:]:
        fields = line.split( "," )
        # remove Fields 2, 6, and 7
        fields = fields[0:2] + fields[3:6] + fields[8:]

        # display the newly formed lines
        print( ", ".join( fields ) )

main()


Inheritance0.py


  • A program that displays a moving car. Need to add a Truck class...


# inheritance0.py
# A simple car class to start with.
# To do:  add a truck class, which is a car
# with a top.

from graphics import *

WIDTH = 600
HEIGHT = 400

class Wheel:
    """a Wheel is 2 concentric circles, the larger one black,
    the smaller one grey."""

    def __init__(self, centr, rad1, rad2 ):
        # make circ1 the smaller of the 2 circles
        self.circ1   = Circle( centr, min( rad1, rad2 ) )
        self.circ2   = Circle( centr, max( rad1, rad2 ) )
        self.circ1.setFill( "grey" )
        self.circ2.setFill( "black" )
        
    def draw( self, win ):
        self.circ2.draw( win )
        self.circ1.draw( win )

    def move( self, dx, dy ):
        self.circ2.move( dx, dy )
        self.circ1.move( dx, dy )
        
class Car:
    """A default simple car class.   Sets the color to "Yellow" """
    def __init__( self, rp ):
        """builds a car 180 pixels long on a reference point"""
        x = rp.getX()
        y = rp.getY()
        length = 180           # geometry of the car
        height = length // 3   
        firstQuarter = length//4
        thirdQuarter = length * 3 // 4
        p2 = Point( x+length, y+height )
        self.body = Rectangle( rp, p2 )
        self.body.setFill( "yellow" )
        self.w1   = Wheel( Point( x+firstQuarter, y+height ), 20,10 )
        self.w2   = Wheel( Point( x+thirdQuarter, y+height ), 20,10 )

    def draw( self, win ):
        """ draws the body and wheels on the window"""
        self.body.draw( win )
        self.w1.draw( win )
        self.w2.draw( win )

    def move( self, dx, dy ):
        """ moves the body and wheels some delta x and y"""
        self.body.move( dx, dy )
        self.w1.move( dx, dy )
        self.w2.move( dx, dy )

                  
def main():
    # open the window
    win = GraphWin( "CSC111 Inheritance Demo", WIDTH, HEIGHT )

    car = Car( Point( 100,250 ) )
    car.draw( win )

    # animation loop
    while win.checkMouse()==None:
        car.move( 2, 0 )
        
    win.close()

main()



</onlydft>