Contents
- birthyear.py
- evenorodd.py
- speedingticket.py
birthyear.py 1/3
[top][prev][next]
# This program calculates your birthyear,
# given your age and the current year.
# Sara Sprenkle
print("This program determines your birth year")
print("given your age and current year")
print()
age = eval(input("Enter your age: "))
if age > 120:
print("Don't be ridiculous, you can't be that old.")
else:
currentYear = eval(input("Enter the current year: "))
birthyear = currentYear - age
print()
print("You were either born in", birthyear, end='')
print("or", birthyear-1)
print("Thank you. Come again.")
evenorodd.py 2/3
[top][prev][next]
# This program determines whether a number is even or odd
# Sara Sprenkle
x = eval(input("Enter a number: "))
remainder = x%2
if remainder == 0:
print(x, "is even")
if remainder == 1:
print(x, "is odd")
# alternatively, we should use an "else" statement
# instead of the second if statement because it is more efficient.
# In this case, we are only doing one check of a condition.
# If the condition is false, then execute the else.
if remainder == 0:
print(x, "is even")
else:
print(x, "is odd")
speedingticket.py 3/3
[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 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
"""
if limit>=speed:
return 0
mphOver = speed-limit
fine = mphOver*5+50
if speed > 90:
fine = fine + 200
return fine
def calculateFine2(speedlimit, clockedspeed):
# another implementation of the function
if clockedspeed > speedlimit:
mphOver = clockedspeed-speedlimit
fine = mphOver*5 + 50
if clockedspeed > 90:
fine += 200
return fine
else:
return 0
def testCalculateFine():
#not speeding
test.testEqual(calculateFine(35, 35), 0)
test.testEqual(calculateFine(35, 34), 0)
test.testEqual(calculateFine(100, 91), 0)
# speeding
test.testEqual(calculateFine(35, 36), 55)
test.testEqual(calculateFine(35, 40), 75)
test.testEqual(calculateFine(70, 90), 150)
# aggressive speeding
test.testEqual(calculateFine(50, 100), 500)
test.testEqual(calculateFine(70, 91), 355)
testCalculateFine()
Generated by GNU Enscript 1.6.6.