Contents
- arithmetic_original.py
- arithmetic.py
- arithmetic_solution.py
- oldmac.py
- winpercent.py
arithmetic_original.py 1/5
[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/5
[top][prev][next]
# In the process of refactoring...
# Compute the result of i ^ 2 + 3 j - 50
# CSCI111
import test
def main():
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 = calculateFormula(i, j)
print("The result of i^2 + 3j - 5 = ", result)
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 )
test.testEqual( calculateFormula(0, 0), -5 )
test.testEqual( calculateFormula(-1, 2), 2 )
test.testEqual( calculateFormula(2, 1.5), 3.5 )
#testCalculateFormula()
main()
arithmetic_solution.py 3/5
[top][prev][next]
# Compute the result of i ^ 2 + 3 j - 50
# CSCI111
import test
def main():
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 = evaluateFormula(i, j)
print("The result of i^2 + 3j - 5 = ", result)
def evaluateFormula( i, j ):
"""
Given two numbers, i and j (floats),
calculate and return the result of the formula i^2 + 3j - 5.
"""
result = i**2 + 3*j - 5
return result
def testEvaluateFormula():
# add the tests...
test.testEqual(evaluateFormula(7, 2), 50)
#testEvaluateFormula()
main()
oldmac.py 4/5
[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()
if __name__ == '__main__':
main()
winpercent.py 5/5
[top][prev][next]
# Calculates a team's winning percentage
# By CSCI111
import test
def main():
print("This program calculates your team's win percentage")
teamWins = eval(input("How many wins are there? "))
teamLosses = eval(input("How many losses are there? "))
pct = calculateWinPct(teamWins, teamLosses)
print("Your team's win percentage is", pct)
def calculateWinPct(wins, losses):
"""
Precondition: wins and losses are both non-negative integers
(if both wins and losses are 0, will cause error).
Postcondition: calculates and returns the win percentage,
which will be a float such that 1 >= percentage >=0
"""
totalGames = wins + losses
pct = wins/totalGames
return pct
def testCalculateWinPct():
test.testEqual( calculateWinPct(5, 15), .25)
test.testEqual( calculateWinPct(1, 1), .5)
test.testEqual( calculateWinPct(5, 0), 1.0)
test.testEqual( calculateWinPct(0, 5), 0.0)
#testCalculateWinPct()
main()
Generated by GNU Enscript 1.6.6.