Contents

  1. ./broken_speeding_ticket.py
  2. ./grade_elif.py
  3. ./grade_function.py
  4. ./grade.py
  5. ./multiples.py
  6. ./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
#
# 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/6

[
top][prev][next]
# Compute the letter grade, based on the numeric grade.
# 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", letter_grade)

./grade_function.py 3/6

[
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 = determineLetterGrade(numericGrade)
    print("Your letter grade is", letter_grade)

def determineLetterGrade( 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( determineLetterGrade(110), "A" )
    test.testEqual( determineLetterGrade(95), "A" )
    test.testEqual( determineLetterGrade(90), "A" )
    test.testEqual( determineLetterGrade(89.99999), "B" )
    test.testEqual( determineLetterGrade(85), "B" )
    test.testEqual( determineLetterGrade(80), "B" )
    test.testEqual( determineLetterGrade(79.99999), "C" )
    test.testEqual( determineLetterGrade(71), "C" )
    test.testEqual( determineLetterGrade(70), "C" )
    test.testEqual( determineLetterGrade(69.99999), "D" )
    test.testEqual( determineLetterGrade(68), "D" )
    test.testEqual( determineLetterGrade(60), "D" )
    test.testEqual( determineLetterGrade(59.99999), "F" )
    test.testEqual( determineLetterGrade(0), "F" )
    test.testEqual( determineLetterGrade(-20), "F" )
    

testDetermineLetterGrade()

#main()

./grade.py 4/6

[
top][prev][next]
# Compute the letter grade, based on the numeric grade.
# Written using a function.
# By CSCI 111

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

if numericGrade >= 90:
    letterGrade = "A"
else:
    # if we reach here, we know that numericGrade < 90
    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/6

[
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/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
# TODO: main
# CSCI 111

import test

def main():
    print("This program determines whether you were speeding and your fine,")
    print("if appropriate.")
    
    # getting the necessary input from the user
    clockedSpeed = eval(input("Enter your speed: "))
    speedLimit = eval(input("Enter the speed limit: "))
    
    # your code here
    fine = calculateFine(speedLimit, clockedSpeed)
    if fine > 0:    
        print("Your fine is $",  fine )
    else:
        print("You weren't speeding!")
    
    
    
def calculateFine(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 or 0 if not speeding
    '''
    ticket = 0
    if speed > limit: # if they're speeding
        ticket = 50
        ticket += (speed - limit) * 5
        if speed > 90: # if they're excessively speeding
            ticket += 200
    return ticket
    

def calculateFine2(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 or 0 if not speeding
    """
    if speed > limit:
        if speed > 90:
            return 5 * (speed-limit) + 250
        return 5 * (speed-limit) + 50
    return 0
    """
    if speed <= limit:
        return 0
    else:
        # compute the fine
        if speed > 90:
            return 5 * (speed-limit) + 250
        return 5 * (speed-limit) + 50
    """
    
def testCalculateFine():
    #not speeding
    test.testEqual(calculateFine(35, 34), 0)
    test.testEqual(calculateFine(90, 90), 0)
    test.testEqual(calculateFine(100, 99), 0)


    # speeding
    test.testEqual(calculateFine(35, 40), 75)
    test.testEqual(calculateFine(80, 90), 100)
    
    # aggressive speeding
    test.testEqual(calculateFine(50, 95), 475)
    
    
#testCalculateFine()
main()

Generated by GNU Enscript 1.6.5.90.