Contents
- average2.py
- average2_withmain.py
- circleShiftAnim2.py
- circleShiftAnim3.py
- circleShiftAnim.py
- mystery.py
- oldmac.py
- test.py
- testSumEvens.py
average2.py 1/9
[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()
num1 = eval(input("Enter the first number: " ))
num2 = eval(input("Enter the second number: "))
# calculate the average of the two numbers
average = average2(num1, num2)
print("The average of", num1, "and", num2, "is", average)
average2_withmain.py 2/9
[top][prev][next]
# Program to find the average of two numbers.
# Demonstrates using a main function.
# by Sara Sprenkle
def main():
print("This program will find the average of two numbers.")
print()
num1 = eval(input("Enter the first number: " ))
num2 = eval(input("Enter the second number: "))
# calculate the average of the two numbers
average = average2(num1, num2)
print("The average of", num1, "and", num2, "is", average)
def average2(num1, num2):
"""
Parameters: two numbers to be averaged
Returns the average of two numbers
"""
average = (num1 + num2)/2
return average
main()
circleShiftAnim2.py 3/9
[top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times.
# Another version of the program, with a different refactoring
# by CSCI 111
from graphics import *
from time import sleep
STEPS = 100
NUM_TIMES = 5
def main():
win = GraphWin("My Circle", 500, 500)
centerPoint = Point(25, 25)
circle = Circle(centerPoint, 25)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)
# tell the user that they need to click somewhere --
instruction = Text( anchorPoint, "Click where you want the circle to go")
instruction.draw(win)
for time in range(NUM_TIMES):
# the user picks the new center
destPoint = win.getMouse()
# animate the circle to that new location
animateCircle( circle, destPoint )
win.getMouse()
win.close()
def animateCircle( circle, newCenter ):
"""
Animates moving the circle from its current position to the
new location.
circle: the Circle object to be moved
newCenter: the Point object where the circle should end up.
"""
# what is the total distance we need to go,
# in both the x and y direction?
centerPoint = circle.getCenter()
changeInX = newCenter.getX() - centerPoint.getX()
changeInY = newCenter.getY() - centerPoint.getY()
# animate to that new position.
for step in range(STEPS):
circle.move(changeInX/STEPS, changeInY/STEPS)
sleep(.01)
main()
circleShiftAnim3.py 4/9
[top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times.
# Another version of the program, with a different refactoring
# by CSCI 111
from graphics import *
from time import sleep
STEPS = 100
def main():
win = GraphWin("My Circle", 500, 500)
centerPoint = Point(25, 25)
circle = Circle(centerPoint, 25)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)
# tell the user that they need to click somewhere --
instruction = Text( anchorPoint, "Click where you want the circle to go")
instruction.draw(win)
repeatAnimatingCircleToUserClick(circle, win, 5)
win.getMouse()
win.close()
def repeatAnimatingCircleToUserClick(circle, window, numRepeats):
"""
Animates moving the circle to where the user clicked a number of times.
circle - the Circle object to be moved
window - the GraphWin where the circle is drawn and the user clicks
numRepeats - the number of times to repeat the user click and animation
"""
for time in range(numRepeats):
# the user picks the new center
destPoint = window.getMouse()
# animate the circle to that new location
animateCircle( circle, destPoint )
def animateCircle( circle, newCenter ):
"""
Animates moving the circle from its current position to the
new location.
circle: the Circle object to be moved
newCenter: the Point object where the circle should end up.
"""
# what is the total distance we need to go,
# in both the x and y direction?
centerPoint = circle.getCenter()
changeInX = newCenter.getX() - centerPoint.getX()
changeInY = newCenter.getY() - centerPoint.getY()
# animate to that new position.
for step in range(STEPS):
circle.move(changeInX/STEPS, changeInY/STEPS)
sleep(.01)
main()
circleShiftAnim.py 5/9
[top][prev][next]
# Animate moving a circle to the position clicked by the user 5 times.
# Refactored the program to use functions that we defined.
# by CSCI 111
from graphics import *
from time import sleep
STEPS = 100
NUM_TIMES = 5
def main():
win = GraphWin("My Circle", 500, 500)
centerPoint = Point(25, 25)
circle = Circle(centerPoint, 25)
circle.draw(win)
anchorPoint = Point(win.getWidth()/2, 10)
# tell the user that they need to click somewhere --
instruction = Text( anchorPoint, "Click where you want the circle to go")
instruction.draw(win)
for time in range(NUM_TIMES):
centerPoint = circle.getCenter()
# the user picks the new center
destPoint = win.getMouse()
# what is the total distance we need to go,
# in both the x and y direction?
changeInX = destPoint.getX() - centerPoint.getX()
changeInY = destPoint.getY() - centerPoint.getY()
animateCircle( circle, changeInX, changeInY )
win.getMouse()
win.close()
def animateCircle( circle, dx, dy ):
"""
Animate the circle, moving dx pixels in the x direction and
dy pixels in the y direction, over STEPS steps.
circle: the Circle object to be moved
dx: an integer representing the number of pixels to move in the x direction,
dy: an integer representing the number of pixels to move in the y direction.
"""
for step in range(STEPS):
circle.move(changeInX/STEPS, changeInY/STEPS)
sleep(.01)
main()
mystery.py 6/9
[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 7/9
[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 verse 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")
# prints a verse of Old MacDonald, plugging in the animal and sound
# parameters (which are strings), as appropriate.
def printVerse(animal, sound):
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()
test.py 8/9
[top][prev][next]
# From How to Think Like a Computer Scientist textbook
def testEqual(actual, expected):
if type(expected) == type(1):
# they're integers, so check if exactly the same
if actual == expected:
print('Pass')
return True
elif type(expected) == type(1.11):
# a float is expected, so just check if it's very close, to allow for
# rounding errors
if abs(actual-expected) < 0.00001:
print('Pass')
return True
else:
# check if they are equal
if actual == expected:
print('Pass')
return True
print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
return False
testSumEvens.py 9/9
[top][prev][next]
# Demonstrate testing sumEvens
# by CSCI111
import test
def main():
test.testEqual( sumEvens( 10 ), 20)
# what are other good tests?
def sumEvens(limit):
""" Adds up the even numbers, up to but not including
the limit (an integer).
Returns that sum."""
total = 0
for x in range(0, limit, 2):
total += x
return total
main()
Generated by GNU Enscript 1.6.6.