Contents

  1. animate.py
  2. average2.py
  3. circle_move.py
  4. circleShiftAnim.py
  5. circleShiftWithFunction.py
  6. function_example.py
  7. pick4.py

animate.py 1/7

[
top][prev][next]
# Simple demonstration of animation.
# by Sara Sprenkle

from graphics import *
from time import sleep

STEPS = 100

w = GraphWin("Simple Animation", 400, 400)
w.setBackground("orange")

current = Point(60,60)
circ = Circle(current, 50)
circ.setFill("blue")
circ.draw(w)

end = w.getWidth()
dx = (end - current.getX())/STEPS

for step in range(STEPS):
    circ.move(dx, 0)
    sleep(.1)

w.getMouse()
w.close()


average2.py 2/7

[
top][prev][next]
# Program to find the average of two numbers
# by Sara Sprenkle

def average2(num1, num2):
    """
    Parameters: two numbers to be averaged
    Returns the average of two numbers
    """
    average = (num1 + num2)/2
    return average
    
print("This program will find the average of two numbers.")
print()

userInput1 = eval(input("Enter the first number: " ))
userInputTwo = eval(input("Enter the second number: "))

# calculate the average of the two numbers
avg = average2(userInput1, userInputTwo)

print("The average of", userInput1, "and", userInputTwo, "is", avg)

circle_move.py 3/7

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

from graphics import *

CIRCLE_RADIUS = 50

canvas = GraphWin("Move the Circle", 500, 500)

# create the circle in the upper left
myCircle = Circle( Point(50,50), 50)
myCircle.setFill("fuchsia")
myCircle.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)

for something in range(3):
    # 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 - myCircle.getCenter().getX()
    dy = newY - myCircle.getCenter().getY()
     
    myCircle.move(dx, dy)

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

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

circleShiftAnim.py 4/7

[
top][prev][next]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111

from graphics import *
from time import sleep

CIRCLE_RADIUS = 50
STEPS = 50

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

# create the initial circle in the center of the window and draw it
midPoint = Point(canvas.getWidth()/2, canvas.getHeight()/2)
circle = Circle(midPoint, CIRCLE_RADIUS)
circle.setFill("chartreuse")
circle.setOutline("limegreen")
circle.setWidth(3)
circle.draw(canvas)
anchorPoint = Point(canvas.getWidth()/2, 10)

# Give instructions to the user
directions = Text(anchorPoint, "Click where you want your circle to go.")
directions.draw(canvas)

# repeat the whole process 5 times    
for i in range(5):
    new_point = canvas.getMouse()
    
    # how far the circle needs to travel
    differenceInX = new_point.getX() - circle.getCenter().getX()
    differenceInY = new_point.getY() - circle.getCenter().getY()
    
    # animate moving the circle by moving in small increments STEPS many times,
    # alternated with sleeping
    for whatever in range(STEPS):
        circle.move( differenceInX/STEPS, differenceInY/STEPS )
        sleep(.05)

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

canvas.getMouse()
canvas.close()

circleShiftWithFunction.py 5/7

[
top][prev][next]
# Move a circle to the position clicked by the user 5 times
# by CSCI 111

from graphics import *

CIRCLE_RADIUS = 50

def moveCircle( circle, newCenter ):
    """
    Move the given Circle circle to be centered
    at the Point newCenter
    """
    centerPoint = circle.getCenter()

    differenceInX = newCenter.getX() - centerPoint.getX()
    differenceInY = newCenter.getY() - centerPoint.getY()

    circle.move(differenceInX,differenceInY)   


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

# create the initial circle in the center of the window and draw it
circleCenter = Point(CIRCLE_RADIUS, CIRCLE_RADIUS)
myCircle = Circle(circleCenter, CIRCLE_RADIUS)
myCircle.setFill("fuchsia")
myCircle.draw(canvas)
anchorPoint = Point(canvas.getWidth()/2, 20)

# Give instructions to the user
directions = Text(anchorPoint, "Click where you want your circle to go.")
directions.draw(canvas)

for something in range(3):
    # get where the user clicked
    new_point = canvas.getMouse()
    
    moveCircle( myCircle, new_point )

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

canvas.getMouse()
canvas.close()

function_example.py 6/7

[
top][prev][next]
# Examples using built-in functions
# Sara Sprenkle

x = 6.817454321
#x = 5.6512542

print("We start with x having value", x)

# Call the function round with input x
# Then, save output of function call in variable roundedXInt
roundedXInt = round(x)
print("x rounded to the nearest int:", roundedXInt)

roundedXTenth = round(x, 1)
print("x rounded to the nearest tenth:", roundedXTenth)

a = round(x, 2)
print("x rounded to the nearest hundredth:", a)
# demonstrating that the name doesn't matter,
# but good names make the code easier to understand

roundx = round(x, 3)
print("x rounded to the nearest thousandth:", roundx)

print(round(x, 4))

print("-"*40)
print("x is of", type(x))

pick4.py 7/7

[
top][prev][next]
# Display the numbers that are selected by the magic 
# ping-pong ball machine for Pick4 VA Lottery, all on one line
# By CSCI111

import random

print("This program displays the winning Pick4 Lottery number!")

for x in range(4):
    print(random.randint(0,9), end=" ")
    
print()

Generated by GNU Enscript 1.6.6.