Contents
- eval_cond.py
- grade_elif.py
- grade_notes.py
- grade.py
- speedingticket2.py
- speedingticket_nocomments.py
- speedingticket.py
eval_cond.py 1/7
[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/7
[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 3/7
[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 4/7
[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 5/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
#
# 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: "))
# check if speeding
if speed > speedlimit:
fine = 50 + 5*(speed-speedlimit)
if speed > 90:
fine += 200
print("Your fine will be $", fine)
else:
print("You are not speeding.")
print("Continue safe driving practices.")
speedingticket_nocomments.py 6/7
[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 7/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
# 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: "))
# check if not speeding
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)
Generated by GNU Enscript 1.6.6.