Contents

  1. eval_cond.py
  2. grade_elif.py
  3. grade.py
  4. speedingticket.py

eval_cond.py 1/4

[
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 2/4

[
top][prev][next]
# Compute the letter grade, based on the numeric grade.
# Added checking for erroneous input.
# CSCI 111

# set up the constants for the bounds on the numeric score
MIN_GRADE=0
MAX_GRADE=100

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

# make sure that the user entered a number in the valid range
if grade >= MIN_GRADE and grade <= MAX_GRADE:

    # The following code computes the letter grade from 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("The letter grade is", letter_grade)

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

else:
    print( "The grade must be between", MIN_GRADE, "and", MAX_GRADE )

grade.py 3/4

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

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

# The following code computes the letter grade from the numeric 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.

if grade >= 90:
    letter_grade = "A"
else:
    if grade >= 80:
        letter_grade = "B"
    else:
        # What do we know about the grade at this point?
        if grade >= 70:
            letter_grade = "C"
        else:
            ### Here we know that grade < 70
            if grade >= 60:
                letter_grade = "D"
            else:
                letter_grade = "F"
        
print("The letter grade is", letter_grade)



speedingticket.py 4/4

[
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.
# Our program
# 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, 9/28/2012

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 speed > speedlimit:
    # you're speeding
    # calculate the fine
    over = speed - speedlimit
    fine = 50 + over*5
    
    # reckless driving
    if speed > EXCESSIVE_SPEED:
        fine = fine + 200
    print("You're driving too fast!")
    print("Your fine is $", fine)
else:
    # not speeding!
    print("Continue safe driving practices")
    

Generated by GNU enscript 1.6.4.