Contents
- broken_speeding_ticket.py
- eval_cond.py
- grade_elif.py
- grade_function.py
- grade.py
- speedingticket.py
broken_speeding_ticket.py 1/6
[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
#
# This program is equivalent to speedingticket.py
# but shows an alternative structure for the if/else
#
# By CSCI 111
print("This program determines whether you were speeding and your fine,")
print("if appropriate.")
# getting the necessary input from the user
speed = eval(input("Enter your speed: "))
speedlimit = eval(input("Enter the speed limit: "))
# what happens in this solution? Why is it not correct behavior?
if speed > speedlimit:
fine = 50 + 5*(speed-speedlimit)
print("Your fine will be $", fine)
if speed > 90:
fine += 200
print("Your fine will be $", fine)
if speed <= speedlimit:
print("You are not speeding.")
print("Continue safe driving practices.")
eval_cond.py 2/6
[top][prev][next]
# Examples of advanced conditionals
# by Sara Sprenkle
x = 2
y = 3
z = 4
b = x==2
c = not b
# Note: because of precedence, we don't _need_ parentheses
# I added parentheses for readability/emphasis
d = (y<4) and (z<3)
print("d =", d)
d = (y<4) or (z<3)
print("d =", d)
d = not d
print(b, c, d)
grade_elif.py 3/6
[top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111
numericGrade = float(input("Enter the numeric grade: "))
if grade >= 90:
letter_grade = "A"
elif grade >=80:
letter_grade = "B"
elif grade >= 70:
letter_grade = "C"
elif grade >= 60:
letter_grade = "D"
else:
letter_grade = "F"
print("Your letter grade is", letter_grade)
# Assign a letter grade and then fill into the print statement.
# This approach reduces the duplication of print statements.
# If we need to change a print statement, we'd have a lot of places to change.
grade_function.py 4/6
[top][prev][next]
# Compute the letter grade, based on the numeric grade.
# Written using a function.
# By CSCI 111
def main():
numericGrade = float(input("Enter the numeric grade: "))
letter_grade = determineLetterGrade(numericGrade)
print("Your letter grade is", letter_grade)
def determineLetterGrade( numGrade ):
"""
Given a numeric grade (between 0 and 100, inclusive),
return the letter grade
"""
# Alternatively, you could have a bunch of return statements,
# replacing each time you assign the letter grade,
# but then the nesting with the elses is not required,
# and I wanted you to see that nesting.
# So, that is why we did not solve this (initially) using a function.
if grade >= 90:
letter_grade = "A"
else:
if grade >=80:
letter_grade = "B"
else:
if grade >= 70:
letter_grade = "C"
else:
if grade >= 60:
letter_grade = "D"
else:
letter_grade = "F"
return letter_grade
grade.py 5/6
[top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111
numericGrade = float(input("Enter the numeric grade: "))
if grade >= 90:
letter_grade = "A"
else:
if grade >=80:
letter_grade = "B"
else:
if grade >= 70:
letter_grade = "C"
else:
if grade >= 60:
letter_grade = "D"
else:
letter_grade = "F"
print("Your letter grade is", letter_grade)
# Assign a letter grade and then fill into the print statement.
# This approach reduces the duplication of print statements.
# If we need to change a print statement, we'd have a lot of places to change.
speedingticket.py 6/6
[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
# CSCI 111
import test
EXCESSIVE_SPEED=90
def main():
print("This program determines whether you were speeding and your fine,")
print("if appropriate.")
# getting the necessary input from the user
speed = eval(input("Enter your speed: "))
speedlimit = eval(input("Enter the speed limit: "))
# calculate fine
totalFine = calculateFine( speedlimit, speed )
# display the fine or lack thereof
if totalFine > 0:
print("The fine is $", totalFine)
else:
print("Continue safe driving practices!")
def calculateFine(speedlimit, clockedspeed):
"""
Precondition: speedlimit and clockedspeed are both non-negative integers
Calculates and returns a fine if clockedspeed is greater than the speedlimit, or 0 if not speeding.
"""
if speedlimit < clockedspeed:
# speeding
diff = clockedspeed - speedlimit
fine = 50 + (diff * 5)
if clockedspeed > EXCESSIVE_SPEED:
fine = fine + 200
return fine
else: # not speeding
fine = 0
return fine
def testCalculateFine():
"""
The test cases we discussed in class
"""
# excessive speeding
test.testEqual( calculateFine(50, 100), 500)
# not-quite excessive speeding
test.testEqual( calculateFine(65, 90), 175)
# "typical" speeding
test.testEqual( calculateFine(45, 50), 75)
# not speeding
test.testEqual( calculateFine(70, 65), 0)
# not speeding, same as speed limit
test.testEqual( calculateFine(70, 70), 0)
# not speeding over 90
test.testEqual( calculateFine(95, 95), 0)
#testCalculateFine()
main()
Generated by GNU Enscript 1.6.6.