Contents

  1. ./circleShift.py
  2. ./circleShiftWithFunction.py
  3. ./mystery.py
  4. ./oldmac.py
  5. ./stealing_HOF.py
  6. ./stealing_HOF_with_main.py

./circleShift.py 1/6

[
top][prev][next]
# Move a circle to where the user clicked
# By CS111

from graphics import *

CIRCLE_RADIUS = 50

canvas = GraphWin("Circle Shift", 500, 500)

# create the circle in the upper left
cir_point = Point(CIRCLE_RADIUS, CIRCLE_RADIUS)
my_circle = Circle(cir_point, CIRCLE_RADIUS)
my_circle.setFill("fuchsia")
my_circle.draw(canvas)

# put the directions in the top center of the canvas
directions = Text( Point(250, 20), "Click where you want to move the circle to.")
directions.draw(canvas)


# get the info from the click
new_point = canvas.getMouse()
newX = new_point.getX()
newY = new_point.getY()

# need to move the circle to that point, but move requires a dx and dy...
dx = newX - my_circle.getCenter().getX()
dy = newY - my_circle.getCenter().getY()

my_circle.move(dx, dy)


directions.setText("Click to close the window")

# wait for the mouse click
canvas.getMouse()
canvas.close()

./circleShiftWithFunction.py 2/6

[
top][prev][next]
# Move a circle to the position clicked by the user.
# Solution now uses a function to move the circle.
# by CSCI 111

from graphics import *

CIRCLE_RADIUS = 50

def move_circle( circle, new_center ):
    """
    Moves a Circle object to a new location. 
    circle: the Circle to be moved
    new_center: the center point of where circle should be moved
    """
    center_point = circle.getCenter()

    diff_in_x = new_center.getX() - center_point.getX()
    diff_in_y = new_center.getY() - center_point.getY()

    circle.move(diff_in_x, diff_in_y)   


canvas = GraphWin("Circle Shift", 500, 500)

# create the initial circle in the center of the window and draw it
circle_center = Point(CIRCLE_RADIUS, CIRCLE_RADIUS)
my_circle = Circle(circle_center, CIRCLE_RADIUS)
my_circle.setFill("fuchsia")
my_circle.draw(canvas)

# Give instructions to the user
anchor_point = Point(canvas.getWidth()/2, 20)
directions = Text(anchor_point, "Click where you want your circle to go.")
directions.draw(canvas)

# get where the user clicked
new_point = canvas.getMouse()
    
move_circle( my_circle, new_point )
canvas.getMouse()

# For fun: Move the circle to a specific point (the top right)
move_circle( my_circle, Point(500, 0) )


directions.setText("Click to close the window")

canvas.getMouse()
canvas.close()

./mystery.py 3/6

[
top][prev][next]
# Mystery Program
# Used to demonstrate variable lifetimes and scope

def main():
    x = 10
    sum = sumEvens( x )
    print("The sum of even #s up to", x, "is", sum)

def sumEvens(limit):
    total = 0
    for x in range(0, limit, 2):
        total += x	
    return total

main()


./oldmac.py 4/6

[
top][prev][next]
# Print out verses of the song Old MacDonald
# Sara Sprenkle

BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"

def main():
    # call the printVerse function to print out a verse
    printVerse("dog", "ruff")
    printVerse("duck", "quack")
    
    animal_type = "cow"
    animal_sound = "moo"
    
    printVerse(animal_type, animal_sound)

# QUESTION: What happens if main called function as
# printVerse("ruff", "dog")

def printVerse(animal, sound):
    """
    Prints a verse of Old MacDonald, plugging in the animal and sound
    parameters (which are strings), as appropriate.
    """
    print(BEGIN_END, EIEIO)
    print("And on that farm he had a", animal, EIEIO)
    print("With a", sound, ",", sound, "here")
    print("And a ", sound, ",", sound, "there")
    print("Here a", sound)
    print("There a", sound)
    print("Everywhere a", sound, ", ", sound)
    print(BEGIN_END, EIEIO)
    print()


main()


./stealing_HOF.py 5/6

[
top][prev][next]
# Program to compare the stealing percentages of Hall of Famers
# by Sara Sprenkle

def success_percentage(successes, failures):
    """
    Parameters: the number of successes and
    the number of failures (both non-negative integers)
    Returns the success percentage (between 0 and 1)
    """
    total_attempts = successes + failures
    percent_success = successes/total_attempts
    return percent_success


print("Baseball Hall of Famers: Top Stealing Percentages")

henderson_stole = 1406
henderson_caught = 335

brock_stole = 938
brock_caught = 307


henderson_steal_pct = success_percentage(henderson_stole, henderson_caught)
brock_steal_pct = success_percentage(brock_stole, brock_caught)


#print("Henderson's stealing %:", henderson_steal_pct)

# showing that we can display the output from the function in different ways
print("Henderson's stealing %:", round(henderson_steal_pct*100, 2))
print("Brock's stealing %:", round(brock_steal_pct*100, 2))

difference = (henderson_steal_pct - brock_steal_pct)*100

print("Henderson's stealing % is", round(difference, 2), "greater than Brock's")

./stealing_HOF_with_main.py 6/6

[
top][prev][next]
# Program to compare the stealing percentages of Hall of Famers
# Demonstrates using a main function
# by Sara Sprenkle

def main():
    print("Baseball Hall of Famers: Top Stealing Percentages")

    henderson_stole = 1406
    henderson_caught = 335
    
    brock_stole = 938
    brock_caught = 307

    henderson_steal_pct = success_percentage(henderson_stole, henderson_caught)
    brock_steal_pct = success_percentage(brock_stole, brock_caught)

    #print("Henderson's stealing %:", henderson_steal_pct)

    # showing that we can display the output from the function in different ways
    print("Henderson's stealing %:", round(henderson_steal_pct*100, 2))
    print("Brock's stealing %:", round(brock_steal_pct*100, 2))

    difference = (henderson_steal_pct - brock_steal_pct)*100
    
    print("Henderson's stealing % is", round(difference, 2), "greater than Brock's")

    
def success_percentage(successes, failures):
    """
    Parameters: the number of successes and
    the number of failures (both non-negative integers)
    Returns the success percentage (between 0 and 1)
    """
    total_attempts = successes + failures
    percent_success = successes/total_attempts
    return percent_success


main()

Generated by GNU Enscript 1.6.5.90.