Graphics
import time, os, sys
try: # import as appropriate for 2.x vs. 3.x
import tkinter as tk
except:
import Tkinter as tk
#
# Module Exceptions
class GraphicsError(Exception):
"Generic error class for graphics module exceptions."
pass
OBJ_ALREADY_DRAWN
import math
from graphics import *
def sides(a, b, c):
side = (a + b + c) / 2.0
return side
def area(sides1, sides2, sides3, s):
A = math.sqrt(s*(s - sides1)*(s - sides2)*(s - sides3)
return A
def distance(p1, p2):
dx = p2.getX() - p1.getX()
dy = p2.getY(
Timer
import time
class Timer:
"An object that provides dynamically adjusted pauses to maintain
a constant rate of iteration for a loop.
"
def _init_(self, rate):
"Creates a timer for looping at rate iterations per second."
self.interval = 1/rate
self.las
Ball
import math
from graphics import *
class Ball:
"A pong-style bouncing ball"
def _init_(self, window, center, radius, angle):
" create a ball (circle) in the window having the given
center (a point), radius, and angle (the direction of travel in
degre
CS 120
Chapters 3
Due: Friday, September 30
1. [10 points] Two points are specified using the coordinates (x1, y1) and (x2, y2). Write a
program that calculates the slope of a line through two (non-vertical) points entered by the
user.
y 2 y1
slope
x 2 x
# This program is assignment 4, problem 1.
# By: Us!
from graphics import *
import math
def main():
print("This program is assignment 4, problem 1.")
r = float(input("Enter a radius between -10 and 10: ")
y = float(input("Enter a y-intercept between -10 a
Average
def main():
n = eval(input("How many numbers do you have? ")
sum = 0.0
for i in range(n):
x = eval(input("Enter a number > ")
sum = sum + x
print("\nThe average of the numbers is", sum / n)
main()
Average 2
def main():
sum = 0.0
count = 0
moredata
LineSegment
from graphics import *
import math
def main():
win = GraphWin("Line Segment", 400, 400)
message = Text(Point(50,20), "Click on any two spots in the window")
message.draw(win)
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
me
Racqball
from random import random
def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
def printIntro():
print("This program simulates a game of racquetball between two")
print("playe
Projectile
from math import sin, cos, radians
class Projectile:
"Simulates the flight of simple projectiles near the earth's
surface, ignoring wind resistance. Tracking is done in two
dimensions, height (y) and distance (x)."
def _init_(self, angle, veloc
Futval
import graphics
def main():
#Introduction
print("This program plots the growth of a 10-year investment.")
#Get principal and interest rate
principal = eval(input("Enter the initial principal: ")
rate = eval(input("Enter the annualized interest rate
Button
from graphics import *
class Button:
"A button is a labeled rectangle in a window.
It is activated or deactivated with the activate()
and deactivate() methods. The clicked(p) method
returns true if the button is active and p is inside it."
def _ini
Python Programming:
An Introduction to
Computer Science
Chapter 3
Computing with Numbers
Python Programming, 3/e
1
Numeric Data Types
# change.py
#
A program to calculate the value of some change in dollars
def main():
print("Change Counter")
print()
prin