Contents
- ./average2.py
- ./average2_withmain.py
- ./circleShift.py
- ./circleShiftWithFunction.py
- ./mystery.py
- ./oldmac.py
./average2.py 1/6
[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/6
[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()
./circleShift.py 3/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
cirPoint = Point(CIRCLE_RADIUS, CIRCLE_RADIUS)
myCircle = Circle( cirPoint, CIRCLE_RADIUS)
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)
# 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()
./circleShiftWithFunction.py 4/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 moveCircle( circle, newCenter ):
"""
Moves a Circle object a new location.
circle: the Circle to be moved
newCenter: the center point of where circle should be moved
"""
centerPoint = circle.getCenter()
diffInX = newCenter.getX() - centerPoint.getX()
diffInY = newCenter.getY() - centerPoint.getY()
circle.move(diffInX,diffInY)
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)
# Give instructions to the user
anchorPoint = Point(canvas.getWidth()/2, 20)
directions = Text(anchorPoint, "Click where you want your circle to go.")
directions.draw(canvas)
# get where the user clicked
new_point = canvas.getMouse()
moveCircle( myCircle, new_point )
canvas.getMouse()
# For fun: Move the circle to a specific point (the top right)
moveCircle( myCircle, Point(500, 0) )
directions.setText("Click to close the window")
canvas.getMouse()
canvas.close()
./mystery.py 5/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 6/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()
Generated by GNU Enscript 1.6.5.90.