Contents
- average2.py
- circleShiftAnim.py
- circleShift.py
- circleShiftWithFunction.py
- our_favorite_expression.py
average2.py 1/5
[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 the 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)
circleShiftAnim.py 2/5
[top][prev][next]
# Animate moving a circle to where the user clicked
# By CS111
from graphics import *
from time import sleep
CIRCLE_RADIUS = 50
STEPS = 30
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)
for numClicks 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()
# break the total distance the circle needs to travel into a number steps
# travel that many steps
for step in range(STEPS):
# divide distance by number of steps
# every time you move the circle, move by that amount
myCircle.move(dx/STEPS, dy/STEPS)
# in between that, you would sleep
sleep(.05)
directions.setText("Click to close the window")
# wait for the mouse click
canvas.getMouse()
canvas.close()
circleShift.py 3/5
[top][prev][next]
# Move a circle to where the user clicked 3 times
# 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)
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()
circleShiftWithFunction.py 4/5
[top][prev][next]
# Move a circle to the position clicked by the user 3 times
# 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)
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()
our_favorite_expression.py 5/5
[top][prev][next]
# Program computes the answer to our favorite expression: i² + 3j - 5
# By CSCI111
print("This program calculates the result of the expression i^2 + 3j - 5")
def calculate
user_i = float(input("Enter i:"))
user_j = float(input("Enter j"))
Generated by GNU Enscript 1.6.6.