Contents

  1. broken_speeding_ticket.py
  2. eval_cond.py
  3. grade_elif.py
  4. grade_notes.py
  5. grade.py
  6. speedingticket2.py
  7. speedingticket_nocomments.py
  8. speedingticket.py

broken_speeding_ticket.py 1/8

[
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
#
# 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/8

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

[
top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111

print("This program generates the letter grade from the numeric grade.")

# get the input from the user for the numeric grade
numGrade = float(input("Enter the numeric score: "))

# The following code computes the letter grade from the numeric grade.
if numGrade >= 90:
    letterGrade = "A"
elif numGrade >= 80:
    letterGrade = "B"
elif numGrade >= 70:
    letterGrade = "C"
elif numGrade >= 60:
    letterGrade = "D"
else:
    letterGrade = "F"

        
print("The letter grade is", letterGrade)

# Compare using elif to using nested if-else-if statements

grade_notes.py 4/8

[
top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111

numericGrade = float(input("Enter the numeric grade: "))



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 to change.


grade.py 5/8

[
top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111

print("This program generates the letter grade from the numeric grade.")

# get the input from the user for the numeric grade
numGrade = float(input("Enter the numeric score: "))

if numGrade >= 90:
    letterGrade = "A"
else:
    if numGrade >= 80:
        letterGrade = "B"
    else:
        if numGrade >= 70:
            letterGrade = "C"
        else:
            if numGrade >= 60:
                letterGrade = "D"
            else:
                letterGrade = "F"

# display the results        
print("You got a", letterGrade)

# 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 to change.

speedingticket2.py 6/8

[
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
#
# This program is equivalent to speedingticket.py
# but shows an alternative structure for the if/else
#
# CSCI 111

EXCESSIVE_SPEED=90

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: "))

# speeding
if speed > speedlimit:
    # calculate fine
    speedover = speed-speedlimit
    fine = 50 + speedover * 5
    # excessive speeding
    if speed > EXCESSIVE_SPEED:
        fine = fine + 200
    print("Your fine is $", fine)
else:
    print("No ticket. Continue safe driving practices.")
    


speedingticket_nocomments.py 7/8

[
top][prev][next]
EXCESSIVE_SPEED=90

print("This program determines whether you were speeding and your fine,")
print("if appropriate.")

speed = eval(input("Enter your speed: "))
speedlimit = eval(input("Enter the speed limit: "))

if speed <= speedlimit:
    print("You are not speeding.")
    print("Continue safe driving practices.")
else:
    fine = 50 + 5*(speed-speedlimit)
    if speed > EXCESSIVE_SPEED:
        fine += 200
    print("Your fine will be $", fine)











speedingticket.py 8/8

[
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

EXCESSIVE_SPEED=90

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: "))

# if not breaking the speed limit
if speedlimit >= speed:
    print("No ticket. Continue safe driving practices.")
# speeding!
else:
    # calculate fine
    speedover = speed-speedlimit
    fine = 50 + speedover * 5
    # excessive speeding
    if speed > EXCESSIVE_SPEED:
        fine = fine + 200
    print("Your fine is $", fine)




Generated by GNU Enscript 1.6.6.