Contents

  1. ./average2_withtesting.py
  2. ./circleArea_original.py
  3. ./circleArea.py
  4. ./more_winpercent.py
  5. ./our_favorite_expression.py
  6. ./test.py
  7. ./testSumEvens.py
  8. ./winpercent.py

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


./circleArea_original.py 2/8

[
top][prev][next]
# This program finds the area of a circle, given user input
# for the radius of the circle
# original version.
# by CSCI111

import math

print("This program calculates the area of a circle.")
print()

radius=float(input("Enter circle's radius: "))

area=math.pi*radius**2

print()
print("The area of the circle is", round(area,3))

./circleArea.py 3/8

[
top][prev][next]
# This program finds the area of a circle, given user input
# for the radius of the circle.
# Refactored to have functions
# by CSCI111

import math
import test

def main():
    print("This program calculates the area of a circle.")
    print()
    
    user_radius=float(input("Enter circle's radius: "))
    
    area=calculateAreaOfCircle(user_radius)
    
    print()
    print("The area of the circle is", round(area,3))


def calculateAreaOfCircle(radius):
    """
    returns the area of the circle
    radius: the circle's radius as a non-negative float
    """
    area=math.pi*radius**2
    return area
	
	
	
def testCalculateAreaOfCircle():
    test.testEqual(calculateAreaOfCircle(1), 3.14159, 4)
    test.testEqual(calculateAreaOfCircle(2), 12.56636, 4)
    test.testEqual(calculateAreaOfCircle(.5), .785397, 4)



testCalculateAreaOfCircle()
#main()

./more_winpercent.py 4/8

[
top][prev][next]
# Calculate and display a team's win percentage
# for in-conference and over all
# by CSCI111

def main():
    print("This program calculates and displays a team's win percentage,")
    print("both in-conference and overall.")
    
    inConfWins=int(input("How many in-conference wins does the team have? "))
    inConfLosses=int(input("How many in-conference losses does the team have? "))
    
    otherWins=int(input("How many other wins does the team have? "))
    otherLosses=int(input("How many other losses does the team have? "))

    inConfWinPct = calculateSuccessPercentage(inConfWins, inConfLosses)
    
    totalWins = inConfWins + otherWins
    totalLosses = inConfLosses + otherLosses
    overallWinPct = calculateSuccessPercentage(totalWins, totalLosses)
    
    print()
    print("The team's winning percentage is")
    print(inConfWinPct, "% in conference")
    print(overallWinPct, "% overall")


def testCalculateSuccessPercentage():
    test.testEqual( calculateSuccessPercentage(0, 1), 0 )
    test.testEqual( calculateSuccessPercentage(2, 2), .5 )
    test.testEqual( calculateSuccessPercentage(3, 7), .3 )
    test.testEqual( calculateSuccessPercentage(7, 3), .7 )
    test.testEqual( calculateSuccessPercentage(1, 0), 1 )

def calculateSuccessPercentage(successes, failures):
    """
    Returns the success percentage, given the number of 
    successes and failures.
    successes and failures are non-negative integers
    """
    successPct = successes/(successes+failures)*100
    return successPct

main()

./our_favorite_expression.py 5/8

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

./test.py 6/8

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

[
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: 
    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(9), 20)
    test.testEqual( sumEvens( 10 ), 20)

    test.testEqual(sumEvens(-10), 0)
    
    # 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()


./winpercent.py 8/8

[
top][prev][next]
# Calculate and display a team's win percentage
# by CSCI111

print("This program calculates and displays a team's win percentage.")

wins=int(input("How many wins does the team have? "))
losses=int(input("How many losses does the team have? "))

winPct = wins/(wins+losses)*100

print("The team's win percentage is", winPct)

Generated by GNU Enscript 1.6.5.90.