Contents
- circleArea.py
- oldmac.py
- shapes.py
- swap.py
- winpercent.py
circleArea.py 1/5
[top][prev][next]
# This program finds the area of a circle, given user input
# for the radius of the circle.
# Refactored (again) to use a module
# by CSCI111
from shapes import calculateAreaOfCircle
def main():
print("This program calculates the area of a circle.")
print()
user_radius=float(input("Enter circle's radius: "))
area=calculateAreaOfCircle(user_radius)
print()
print("The area of the circle is", round(area,3))
main()
oldmac.py 2/5
[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()
if __name__ == '__main__':
main()
shapes.py 3/5
[top][prev][next]
# Helpful functions relating to shapes!
# TODO: Create functions for finding the area of a rectangle, triangle, ...
# By CSCI 111
import test
import math
def calculateAreaOfCircle(radius):
"""
returns the area of the circle
radius: the circle's radius as a non-negative float
"""
area=math.pi*radius**2
return area
def testCalculateAreaOfCircle():
test.testEqual(calculateAreaOfCircle(1), 3.14159, 4)
test.testEqual(calculateAreaOfCircle(2), 12.56637, 4)
test.testEqual(calculateAreaOfCircle(.5), .7853975, 4)
if __name__ == '__main__':
testCalculateAreaOfCircle()
swap.py 4/5
[top][prev][next]
# Swap the values of two variables
# Shows that immutable data types are passed as *copies*
# By CSCI111
def main():
x = 5
y = 7
print("Before: x =", x, "y =", y)
tmp = x
x = y
y = tmp
print("After: x =", x, "y =", y)
print("\n~~~~~~~~~~~ Using swap function ~~~~~~~~~~~~")
swap(x, y)
print("After swap function: x =", x, "y =", y)
print("\nParameters x and y were copied.")
print("the actual parameters do not change.")
def swap(x, y):
tmp = x
x = y
y = tmp
main()
winpercent.py 5/5
[top][prev][next]
# Calculates a team's winning percentage
# Demonstrates bottom-up development practice
# By CSCI111
import test
def main():
print("This program will calculate your team's win percentage.")
print()
numWins = eval(input("Enter the number of wins: "))
numLosses = eval(input("Enter the number of losses: "))
pctWins = calculateWinPercentage(numWins, numLosses)
print("Your win percentage is", pctWins*100)
# multiplying by 100 to get something more user-friendly.
def calculateWinPercentage(wins, losses):
"""
Calculates and returns a win percentage, based on the given wins
and losses.
Parameters:
- wins: a non-negative integer representing the number of wins
- losses: a non-negative integer representing the number of losses
Pre: either wins or losses must be greater than 1 or will throw a divide
by zero error
Post: returns the win percentage (between 0 and 1, inclusive)
"""
winPct = wins / (wins + losses)
return winPct
def testWinPercentage():
test.testEqual(calculateWinPercentage(0, 1), 0)
test.testEqual(calculateWinPercentage(2, 2), .5)
test.testEqual(calculateWinPercentage(3, 7), .3)
test.testEqual(calculateWinPercentage(1, 0), 1)
# testWinPercentage()
main()
Generated by GNU Enscript 1.6.6.