Contents
- grade_elif.py
- grade_notes.py
- grade.py
- speedingticket2.py
- speedingticket.py
grade_elif.py 1/5
[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 2/5
[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 3/5
[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 4/5
[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.py 5/5
[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.