Contents
- more_winpercent.py
- our_favorite_expression.py
- practice1.py
- practice2.py
- practice3.py
- test.py
- winpercent.py
more_winpercent.py 1/7
[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 successes and failures.
successes and failures are non-negative integers
"""
successPct = successes/(successes+failures)*100
return successPct
main()
our_favorite_expression.py 2/7
[top][prev][next]
# Program computes the answer to our favorite expression: i² + 3j - 5
# By CSCI111
import test
def main():
print("This program calculates the result of the expression i^2 + 3j - 5")
user_i = float(input("Enter i: "))
user_j = float(input("Enter j: "))
total = calculateFavoriteExpression(7, 2)
total = calculateFavoriteExpression(user_i, user_j)
print("The result of the expression i^2 + 3j - 5 is ", total)
def calculateFavoriteExpression(i, j):
"""
Takes two numbers (i and j) and returns the value of the expression
i^2 +3j - 5
"""
result = i**2 + 3*j - 5
return result
# alternatively:
# return i**2 + 3*j -5
def testCalculateFavoriteExpression():
test.testEqual( calculateFavoriteExpression(7, 2), 50)
test.testEqual( calculateFavoriteExpression(0, 0), -5)
test.testEqual( calculateFavoriteExpression(1, .333), -3.001)
#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: "))
squared = square(num)
print("The square is", squared)
print("The original num was", n)
def square(n):
return n * n
main()
practice3.py 5/7
[top][prev][next]
# Further 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/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
winpercent.py 7/7
[top][prev][next]
# Refactored: Calculate and display a team's win percentage
# by CSCI111
import test
def main():
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 = calculateSuccessPercentage(wins, losses)
print("The team's win percentage is", winPct)
def calculateSuccessPercentage(successes, failures):
"""
Returns the success percentage, given the successes and failures.
successes and failures are non-negative integers
"""
successPct = successes/(successes+failures)*100
return successPct
def testSuccessPercentage():
test.testEqual( calculateSuccessPercentage(0, 1), 0 )
test.testEqual( calculateSuccessPercentage(2, 2), 50 )
test.testEqual( calculateSuccessPercentage(3, 7), 30 )
test.testEqual( calculateSuccessPercentage(7, 3), 70 )
test.testEqual( calculateSuccessPercentage(1, 0), 100 )
#testSuccessPercentage()
main()
Generated by GNU Enscript 1.6.6.