Contents
- ./broken_speeding_ticket.py
- ./grade_elif.py
- ./grade_function.py
- ./grade.py
- ./multiples.py
- ./speedingticket.py
- ./test.py
./broken_speeding_ticket.py 1/7
[top][prev][next]
# This is NOT a correct solution. Why is it incorrect?
# What test cases reveal the errors?
#
# Any speed clocked over the limit results in a fine of at least $50, plus $5
# for each mph over the limit, plus a penalty of $200 for any speed over 90mph.
#
# Input: speed limit and the clocked speed
# Output: either (a) that the clocked speed was under the limit or
# (b) the appropriate fine
#
# By CSCI 111
def calculateFine(speed, speedLimit):
# what happens in this solution? Why is it not correct behavior?
if speed > speedLimit:
fine = 50 + 5*(speed-speedLimit)
return fine
if speed > 90:
fine += 200
return fine
if speed <= speedLimit:
return 0
./grade_elif.py 2/7
[top][prev][next]
# Compute the letter grade, based on the numeric grade.
# This solution uses elifs instead of the nested else ifs
# By CSCI 111
numericGrade = float(input("Enter the numeric grade: "))
if numericGrade >= 90:
letterGrade = "A"
elif numericGrade >= 80:
letterGrade = "B"
elif numericGrade >= 70:
letterGrade = "C"
elif numericGrade >= 60:
letterGrade = "D"
else:
letterGrade = "F"
print("Your letter grade is", letterGrade)
./grade_function.py 3/7
[top][prev][next]
# Compute the letter grade, based on the numeric grade.
# Written using a function. Below are a few different options
# for implementation.
# By CSCI 111
import test
def main():
numericGrade = float(input("Enter the numeric grade: "))
letter_grade = determine_letter_grade(numericGrade)
print("Your letter grade is", letter_grade)
def determine_letter_grade( numGrade ):
"""
Given a numeric grade (a float),
return the letter grade (a string)
Parameter:
- numGrade: a float representing the numeric grade
Returns the letter grade associated with the letter grade
"""
if numGrade >= 90:
letter_grade = "A"
else:
if numGrade >=80:
letter_grade = "B"
else:
if numGrade >= 70:
letter_grade = "C"
else:
if numGrade >= 60:
letter_grade = "D"
else:
letter_grade = "F"
return letter_grade
"""
Alternative 1, with "interleaved?" return statements:
if numGrade >= 90:
return "A"
else:
if numGrade >= 80:
return "B"
else:
if numGrade >= 70:
return "C"
else:
if numGrade >= 60:
return "D"
else:
return "F"
"""
"""
Alternative 2, without elses because we know that the return means
exit from the function
if numGrade >= 90:
return "A"
if numGrade >= 80:
return "B"
if numGrade >= 70:
return "C"
if numGrade >= 60:
return "D"
return "F"
"""
def testDetermineLetterGrade():
test.testEqual( determine_letter_grade(110), "A" )
test.testEqual( determine_letter_grade(95), "A" )
test.testEqual( determine_letter_grade(90), "A" )
test.testEqual( determine_letter_grade(89.99999), "B" )
test.testEqual( determine_letter_grade(85), "B" )
test.testEqual( determine_letter_grade(80), "B" )
test.testEqual( determine_letter_grade(79.99999), "C" )
test.testEqual( determine_letter_grade(71), "C" )
test.testEqual( determine_letter_grade(70), "C" )
test.testEqual( determine_letter_grade(69.99999), "D" )
test.testEqual( determine_letter_grade(68), "D" )
test.testEqual( determine_letter_grade(60), "D" )
test.testEqual( determine_letter_grade(59.99999), "F" )
test.testEqual( determine_letter_grade(0), "F" )
test.testEqual( determine_letter_grade(-20), "F" )
testDetermineLetterGrade()
#main()
./grade.py 4/7
[top][prev][next]
# Compute the letter grade, based on the numeric grade.
# Alternative solution using a function in grade_function.py
# By CSCI 111
numericGrade = float(input("Enter the numeric grade: "))
if numericGrade >= 90:
letterGrade = "A"
else:
if numericGrade >= 80:
letterGrade = "B"
else:
if numericGrade >= 70:
letterGrade = "C"
else:
if numericGrade >= 60:
letterGrade = "D"
else:
letterGrade = "F"
print("Your grade is", letterGrade)
./multiples.py 5/7
[top][prev][next]
# Checking multiples
# -- mostly demonstrating use of elif and how the cases are mutually exclusive
# -- Shows an alternative to what is likely the expected behavior.
# by CSCI111
print("This program determines if the input is a multiple of 2 or 3")
print()
x = int(input("Enter x: "))
if x % 2 == 0:
print(x, "is a multiple of 2")
elif x % 3 == 0:
print(x, "is a multiple of 3")
else:
print(x, "is not a multiple of 2 or 3")
print()
print("-"*40)
print("Implement the expected behavior...")
# this code does something different than the above code:
# it shows if a number is evenly divisible by 2 or 3 or neither
isNotDivisibleBy2Or3 = True
if x % 2 == 0:
print(x, "is a multiple of 2")
isNotDivisibleBy2Or3 = False
if x % 3 == 0:
print(x, "is a multiple of 3")
isNotDivisibleBy2Or3 = False
if isNotDivisibleBy2Or3:
print(x, "is not a multiple of 2 or 3")
./speedingticket.py 6/7
[top][prev][next]
# Any speed clocked over the limit results in a fine of at least $50, plus $5
# for each mph over the limit, plus a penalty of $200 for any speed over 90mph.
#
# Input: speed limit and the clocked speed
# Output: either (a) that the clocked speed was under the limit or
# (b) the appropriate fine
#
# By CSCI 111
import test
def main():
print("This program calculates a fine")
clockedSpeed = int(input("Enter your speed: "))
speedLimit = int(input("Enter the speed limit: "))
fine = calculate_fine(speedLimit, clockedSpeed)
if fine == 0:
print("Carry on!")
else:
print("Your fine is $", fine)
def calculate_fine(limit, speed):
"""
Calculates and returns the fine if the speed is greater than the
limit.
Parameters:
- limit: a non-negative integer representing the speed limit
- speed: a non-negative integer representing the speed
Returns the fine ($50 plus $5 for each mph over the limit plus
a penalty of $200 for any speed over 90 mph) or 0 if not speeding
"""
if speed <= limit : # not speeding
fine = 0
# return 0
else: # speeding
extra_fine = (speed - limit) * 5 # calculating the fine above the $50
fine = 50 + extra_fine
if speed > 90: # excessive speeding
fine += 200
# return fine
return fine
def testCalculateFine():
# not speeding
test.testEqual(calculate_fine(0, 0), 0)
test.testEqual(calculate_fine(35, 35), 0)
test.testEqual(calculate_fine(35, 30), 0)
test.testEqual(calculate_fine(50, 50), 0)
test.testEqual(calculate_fine(51, 50), 0)
test.testEqual(calculate_fine(100, 91), 0)
# speeding
test.testEqual(calculate_fine(35, 40), 75)
test.testEqual(calculate_fine(80, 90), 100)
# excessive speeding
test.testEqual(calculate_fine(80, 91), 305)
test.testEqual(calculate_fine(90, 91), 255)
#testCalculateFine()
main()
./test.py 7/7
[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.5.90.