Contents

  1. average2.py
  2. average2_withmain.py
  3. average2_withtesting.py
  4. mystery.py
  5. oldmac.py
  6. our_favorite_expression.py
  7. test.py
  8. testSumEvens.py

average2.py 1/8

[
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/8

[
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/8

[
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/8

[
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/8

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


our_favorite_expression.py 6/8

[
top][prev][next]
# Program computes the answer to our favorite expression: i² + 3j - 5
# We need to define the function and call the function
# By CSCI111

def main():
	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: "))
	
	# call our function
	expression = favExpression(user_i, user_j)
	
	print("When i =", user_i, "and j=", user_j," i^2+3j-5 is", expression)

def favExpression(i, j):
	expression = i**2 + 3*j -5
	return expression

main()

test.py 7/8

[
top][prev][next]
# From How to Think Like a Computer Scientist textbook

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 8/8

[
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.