Contents
- average2.py
- average2_withmain.py
- average2_withtesting.py
- mystery.py
- oldmac.py
- test.py
- testSumEvens.py
average2.py 1/7
[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/7
[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()
average2_withtesting.py 3/7
[top][prev][next]
# Program to find the average of two numbers.
# Demonstrates using a main function.
# by Sara Sprenkle
import test
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
def testAverage2():
"""
Test the average2 function with good test cases.
"""
# here is where the tests go:
testAverage2()
#main()
mystery.py 4/7
[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 5/7
[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()
test.py 6/7
[top][prev][next]
# From How to Think Like a Computer Scientist textbook
def testEqual(actual, expected):
"""
Parameters: actual and expected results for a function.
Displays "Pass" and returns True if the test case passes.
Displays error message, with expected and actual results, and returns False if test case fails.
"""
if type(expected) == type(1):
# they're integers, so check if exactly the same
if actual == expected:
print('Pass')
return True
elif type(expected) == type(1.11):
# a float is expected, so just check if it's very close, to allow for
# rounding errors
if abs(actual-expected) < 0.00001:
print('Pass')
return True
else:
# check if they are equal
if actual == expected:
print('Pass')
return True
print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
return False
testSumEvens.py 7/7
[top][prev][next]
# Demonstrate testing sumEvens function
# by CSCI111
import test
def main():
x = 10
sum = sumEvens( x )
print("The sum of even #s up to", x, "is", sum)
def testSumEvens():
"""
A function to test that the sumEvens function works as expected.
If it does, the function will display "Pass" for each of the test cases.
"""
actual = sumEvens(10)
expected = 20
test.testEqual( actual, expected )
# same test:
test.testEqual( sumEvens( 10 ), 20)
# what are other good tests?
test.testEqual( sumEvens( 11 ), 30)
test.testEqual( sumEvens( 1 ), 0)
test.testEqual( sumEvens( 0 ), 0)
test.testEqual( sumEvens( 100 ), 2450)
test.testEqual( sumEvens( 9 ), 20)
# this one will not pass -- just to show you what happens if a test case fails
test.testEqual( sumEvens( 10 ), 19)
def sumEvens(limit):
""" Adds up the even numbers, up to but not including
the limit (a positive integer).
Returns that sum."""
total = 0
for x in range(0, limit, 2):
total += x
return total
testSumEvens() # I just want to test the function to make sure it works.
#main()
Generated by GNU Enscript 1.6.6.