Contents

  1. animate.py
  2. circleShiftAnim.py
  3. circleShift.py
  4. function_example.py
  5. module_example.py
  6. pick4.py
  7. print_examples.py
  8. random_test.py

animate.py 1/8

[
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()


circleShiftAnim.py 2/8

[
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

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

# create the initial circle in the center of the window and draw it
midPoint = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle(midPoint, CIRCLE_RADIUS)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)

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

# repeat the whole process 5 times    
for i in range(5):
    newCenter = win.getMouse()
    
    # how far the circle needs to travel
    differenceInX = newCenter.getX() - circle.getCenter().getX()
    differenceInY = newCenter.getY() - circle.getCenter().getY()
    
    # break that distance across multiple STEPS
    
    stepDifferenceInX = differenceInX/STEPS
    stepDifferenceInY = differenceInY/STEPS
    
    for step in range(STEPS): 
       circle.move( stepDifferenceInX, stepDifferenceInY )
       sleep(.1)

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

win.getMouse()
win.close()

circleShift.py 3/8

[
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

STEPS = 50

CIRCLE_RADIUS = 50

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

# create the initial circle in the center of the window and draw it
midPoint = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle(midPoint, CIRCLE_RADIUS)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)

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

# LATER: Do this 5 times.

# get where the user clicked
userClicked = win.getMouse()

# Move the circle to where the user clicks
centerPoint = circle.getCenter()

dx = userClicked.getX() - centerPoint.getX()
dy = userClicked.getY() - centerPoint.getY()

circle.move(dx/STEPS,dy/STEPS)
sleep(.05)

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

win.getMouse()
win.close()

function_example.py 4/8

[
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))

module_example.py 5/8

[
top][prev][next]
# Example of importing a module
# by Sara Sprenkle

# Alternative: could use import math
# Would then need to prepend all constants, functions with math.
from math import *

i = 1j

# The equation e^(i pi) + 1 = 0

# with import math statement
# shouldbezero =  math.e ** (i * math.pi) + 1

shouldbezero =  e ** (i * pi) + 1

print("e^(i pi) + 1 equals", shouldbezero)

# practice using functions from modules
print("100^(1/2) =", sqrt(100))

pick4.py 6/8

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

from random import *
# import random <<-- other way to import from random

print("This program generates the winning VA Pick 4 number.")

# repeat four times
for x in range(4):
    # generate a random number between 0 and 9
    randomNum = randint(0, 9)
    # display the random number you generated
    print(randomNum,end="")
print()

print_examples.py 7/8

[
top][prev][next]
# Examples calling the print function
# Sara Sprenkle for CSCI111

print("Hi", "there", "class", sep='; ')
# By default end is "\n" --> called "the new line character"
# means, put the next displayed text on the next line.
print("Put on same", end='')
print("line")

# make end=" " (a space) instead:
print("Put on same", end=' ')
print("line")

random_test.py 8/8

[
top][prev][next]
# Demonstrating random module
# by Sara Sprenkle

import random

NUM_RANDOM = 10
print("This program generates", NUM_RANDOM, "random numbers.")

# Demonstrates that it's a pseudo-random number generator
# If using the same seed, the program generates the same list of
# "random" numbers.

# The following function call sets the seed.
#random.seed(1)

for x in range(NUM_RANDOM):
    print(random.random())
    #print(random.randint( 0, 10))


Generated by GNU Enscript 1.6.6.