Contents

  1. ./practice1.py
  2. ./practice2.py
  3. ./practice3.py
  4. ./practice4.py
  5. ./practice5.py
  6. ./test.py
  7. ./testSumEvens.py

./practice1.py 1/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 2/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 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)
    print("The original num was", n)

def square(n):
    return n * n

main()

./practice4.py 4/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 num * num

main()

./practice5.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(num):
    return num * num

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