Contents
- average2.py
- average2_withmain.py
- circleShiftWithFunction.py
- oldmac.py
average2.py 1/4
[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/4
[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()
circleShiftWithFunction.py 3/4
[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)
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)
myCircle = Circle(midPoint, CIRCLE_RADIUS)
myCircle.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)
# TODO: Repeat getting where the user clicked and moving to it five times
# get where the user clicked
userClicked = win.getMouse()
moveCircle( myCircle, userClicked )
# get where the user clicked a second time
userClicked = win.getMouse()
moveCircle( myCircle2, userClicked2 )
instruction.setText("Click to close the window")
win.getMouse()
win.close()
oldmac.py 4/4
[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()
Generated by GNU Enscript 1.6.6.