Contents
- average2.py
- average2_withmain.py
- average2_withtesting.py
- our_favorite_expression.py
- test.py
- testSumEvens.py
average2.py 1/6
[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/6
[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/6
[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()
our_favorite_expression.py 4/6
[top][prev][next]
# Program computes the answer to our favorite expression: i² + 3j - 5
# By CSCI111
import test
print("This program calculates the result of the expression i^2 + 3j - 5")
#user_i = float(input("Enter i:"))
#user_j = float(input("Enter j"))
def calculateFavoriteExpression(i, j):
"""
"""
result = i**2 + 3*j -5
return result
# alternatively:
# return i**2 + 3*j -5
test.testEqual( calculateFavoriteExpression(7, 2), 50)
test.testEqual( calculateFavoriteExpression(0, 0), -5)
test.testEqual( calculateFavoriteExpression(1, .333), -3.001)
test.py 5/6
[top][prev][next]
def testEqual(actual,expected,places=5):
'''
Does the actual value equal the expected value?
For floats, places indicates how many places, right of the decimal, must be correct
'''
if isinstance(expected,float):
if abs(actual-expected) < 10**(-places):
print('\tPass')
return True
else:
if actual == expected:
print('\tPass')
return True
print('\tTest Failed: expected {} but got {}'.format(expected,actual))
return False
testSumEvens.py 6/6
[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?
# this one will not pass -- just to show you what happens if a test case fails.
# You want all of your test cases to be accurate tests and to pass.
#test.testEqual( sumEvens( 10 ), 19)
def sumEvens(limit):
"""
limit: a positive integer
Sums the even numbers from 0 up to but not including limit
Return that sum.
"""
"""
Returns the sum of even numbers from 0 up to but not including
limit (a positive integer)
"""
"""
Precondition: limit is a postive integer
Postcondition: returns the sum of even numbers from 0 up to but not including
limit (a positive integer)
"""
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.