Contents

  1. arithmetic_original.py
  2. arithmetic.py
  3. practice1.py
  4. practice2.py
  5. practice3.py
  6. test.py

arithmetic_original.py 1/6

[
top][prev][next]
# Compute the result of i ^ 2 + 3 j - 50
# By CSCI111


print("This program will evaluate the formula i^2 + 3j - 5 for you.")

i = float(input("What is the value of i? "))
j = float(input("What is the value of j? "))

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

print("The result of i^2 + 3j - 5 = ", result)

arithmetic.py 2/6

[
top][prev][next]
# In the process of refactoring...
# Compute the result of i ^ 2 + 3 j - 50
# CSCI111

import test

def calculateFormula(i, j):
    """
    Precondition: i and j are numbers
    Postcondition: calculates i ** 2 + 3 * j - 5 and returns it
    """
    result = i ** 2 + 3 * j - 5
    return result

def testCalculateFormula():
    test.testEqual( calculateFormula(7, 2), 50)

testCalculateFormula()
   
"""
print("This program will evaluate the formula i^2 + 3j - 5 for you.")

i = float(input("What is the value of i? "))
j = float(input("What is the value of j? "))

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

print("The result of i^2 + 3j - 5 = ", result)
"""

practice1.py 3/6

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

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

practice3.py 5/6

[
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", computed)
    print("The original num was", num)

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

main()

test.py 6/6

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

def testEqual(actual, expected):
    if type(expected) == type(1):
        # they're integers, so check if exactly the same
        if actual == expected:
            print('Pass')
            return True
    elif type(expected) == type(1.11):
        # a float is expected, so just check if it's very close, to allow for
        # rounding errors
        if abs(actual-expected) < 0.00001:
            print('Pass')
            return True
    else:
        # check if they are equal
        if actual == expected:
            print('Pass')
            return True
    print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
    return False


Generated by GNU Enscript 1.6.6.