Contents

  1. ./average2_withtesting.py
  2. ./our_favorite_expression.py
  3. ./practice1.py
  4. ./practice2.py
  5. ./practice3.py
  6. ./test.py
  7. ./testSumEvens.py

./average2_withtesting.py 1/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()


./our_favorite_expression.py 2/7

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

import test

def main():
    print("This program computes 2*(i^2 + 3j -5)")
    print()
    myI = float(input("Enter the value of i: "))
    myJ = float(input("Enter the value of j: "))
    answer = calculateFavoriteExpression(myI, myJ)*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)

    
#testCalculateFavoriteExpression()
main()

./practice1.py 3/7

[
top][prev][next]
# Exercising your knowledge of variable scope.
#

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)

def square(n):
    return n * n

main()

./practice2.py 4/7

[
top][prev][next]
# Exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    square(num)
    print("The square is", computed)

def square(n):
    computed = n*n
    return computed

main()

./practice3.py 5/7

[
top][prev][next]
# Exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)
    print("The original num was", n)

def square(n):
    return n * n

main()

./test.py 6/7

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

[
top][prev][next]
# Demonstrate 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( 10 ), 20)
    test.testEqual( sumEvens( 11 ), 30)
    test.testEqual( sumEvens( 12 ), 30)

    # 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.5.90.