Contents
- birthyear.py
- grades.elif.py
- grades.py
- speedingticket2.py
- speedingticket.py
birthyear.py 1/5
[top][prev][next]
# Demonstrate validating user input
# Modified from a student's code from lab assignment
print "This program determines your birth year"
print "given your age and current year"
print
age = input("Enter your age >> ")
if age > 110:
print "Don't be ridiculous, you can't be that old."
else:
currentYear = input("Enter the current year >> ")
if currentYear < 2000:
print "error message"
else:
birthyear = currentYear - age
print
print "You were either born in", birthyear, "or",
print birthyear-1
grades.elif.py 2/5
[top][prev][next]
# Convert numeric grade to letter grade
# By CSCI111
import sys
numScore = input("What is the numeric score (0-100)? ")
# Make sure that they entered a valid score
if numScore < 0 or numScore > 100:
print "The numeric score must be between 0 and 100."
print "Please try again."
sys.exit(0)
if numScore < 60:
letterGrade = "F"
elif numScore < 70:
letterGrade = "D"
elif numScore < 80:
letterGrade = "C"
elif numScore < 90:
letterGrade = "B"
else:
letterGrade = "A"
# Output the letter grade in a grammatically correct way
if letterGrade == "A" or letterGrade == "F":
print numScore, "is an", letterGrade
else:
print numScore, "is a", letterGrade
grades.py 3/5
[top][prev][next]
# Convert numeric grade to letter grade
# By CSCI 111
import sys
numScore = input("What is the numeric score (0-100)? ")
# Make sure that they entered a valid score
if numScore < 0 or numScore > 100:
print "The numeric score must be between 0 and 100."
print "Please try again."
sys.exit(0)
if numScore < 60:
letterGrade = "F"
else:
if numScore < 70:
letterGrade = "D"
else:
if numScore < 80:
letterGrade = "C"
else:
if numScore < 90:
letterGrade = "B"
else:
letterGrade = "A"
# Output the letter grade in a grammatically correct way
if letterGrade == "A" or letterGrade == "F":
print numScore, "is an", letterGrade
else:
print numScore, "is a", letterGrade
speedingticket2.py 4/5
[top][prev][next]
# Calculating Speeding Ticket Fines
# by CSCI 111
speedlimit = input("What was the speed limit? ")
clockedspeed = input("What was your speed? ")
if clockedspeed <= speedlimit:
# not speeding
print "You were not speeding."
print "Continue safe driving practices."
else:
# They were speeding ...
# calculate the amount over the speedlimit
over = clockedspeed - speedlimit
# The minimum fine is $50, add that to the charge for the amount over the speedlimit
overCharge = 5 * over
# check if they were going over 90 mph
if clockedspeed >= 90:
fine = 250 + overCharge
else:
fine = 50 + overCharge
print "Your fine is $%.2f." % fine
speedingticket.py 5/5
[top][prev][next]
# Calculating Speeding Ticket Fines
# by CSCI 111
import sys
speedlimit = input("What was the speed limit? ")
# Make sure that the user input a valid speed limit
if speedlimit < 0:
print "You need to enter a positive speed limit."
sys.exit(1)
clockedspeed = input("What was your speed? ")
# Make sure that the user input a valid clocked speed
if clockedspeed < 0:
print "You need to enter a positive speed."
sys.exit(1)
if clockedspeed <= speedlimit:
# not speeding
print "You were not speeding."
print "Continue safe driving practices."
else:
# They were speeding ...
# calculate the amount over the speedlimit
over = clockedspeed - speedlimit
# $5 for every mph over the speedlimit
overCharge = 5 * over
# The minimum fine is $50, add that to the charge for the amount over the speedlimit
fine = 50 + overCharge
# check if they were going over 90 mph
if clockedspeed >= 90:
fine = 200 + fine
print "Your fine is $%.2f." % fine
Generated by GNU enscript 1.6.4.