Contents

  1. ./average2_withtesting.py
  2. ./oldmac.py
  3. ./orig_favorite_expression.py
  4. ./our_favorite_expression.py
  5. ./test.py
  6. ./testSumEvens.py

./average2_withtesting.py 1/6

[
top][prev][next]
# Program to find the average of two numbers.
# Demonstrates using a main function
# Adds in programmatic testing
# 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()


./oldmac.py 2/6

[
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()


./orig_favorite_expression.py 3/6

[
top][prev][next]
# Program computes the answer to our favorite expression: i^2 + 3j - 5
# By CSCI111

import test

print("This program computes (i^2 + 3j -5)")
print()
i = float(input("Enter the value of i: "))
j = float(input("Enter the value of j: "))

answer = i**2 + 3*j - 5

print()
print("(i^2 + 3j -5) =", answer)


./our_favorite_expression.py 4/6

[
top][prev][next]
# Program computes the answer to our favorite expression: i² + 3j - 5
# and then doubles it!
# By CSCI111

import test

def main():
    print("This program computes 2*(i^2 + 3j -5)")
    print()
    user_i = float(input("Enter the value of i: "))
    user_j = float(input("Enter the value of j: "))
    answer = calculateFavoriteExpression(user_i, user_j)*2
    print("2*(i^2 + 3j -5) =", answer)

def calculateFavoriteExpression(i, j):
    """
    Given numbers for i and j, calculates and returns the
    result of i^2 + 3j -5
    """
    result = i**2 + 3*j - 5
    return result
    
    
def testCalculateFavoriteExpression():
    test.testEqual(calculateFavoriteExpression(0, 0), -5)
    test.testEqual(calculateFavoriteExpression(7, 2), 50)
    test.testEqual(calculateFavoriteExpression(1, 0), -4)
    test.testEqual(calculateFavoriteExpression(-2, -5), -16)
    test.testEqual(calculateFavoriteExpression(1, 1.5), .5, 8)

# uncomment to test our function  
testCalculateFavoriteExpression()
#main()

./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]
# Demonstrates testing sumEvens function
# Also shows a few different examples of doc strings for functions
# 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, written a little differently
    test.testEqual( sumEvens( 10 ), 20)
    
    # what are other good tests?
    test.testEqual( sumEvens( 0 ), 0 )
    test.testEqual( sumEvens( 1 ), 0 )
    test.testEqual( sumEvens( 2 ), 0 )
    test.testEqual( sumEvens( 3 ), 2 )
    test.testEqual( sumEvens( 4 ), 2 )
    test.testEqual( sumEvens( 5 ), 6 )
    test.testEqual( sumEvens( 12 ), 30 )
    

    # Make the last one not pass -- just to show what happens if a 
    # test case fails.
    # When you write your test cases, all of them should be accurate/should 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 i in range(0, limit, 2):
        total += i
    return total

testSumEvens() # I just want to test the function to make sure it works.
#main()


Generated by GNU Enscript 1.6.5.90.