Contents

  1. birthyear.py
  2. broken_speeding_ticket.py
  3. coinflip.py
  4. evenorodd.py
  5. speedingticket2.py
  6. speedingticket.py

birthyear.py 1/6

[
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.")


broken_speeding_ticket.py 2/6

[
top][prev][next]
# This is NOT a correct solution.  Why is it incorrect?
# What test cases reveal the errors?
# 
# 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: "))

# what happens in this solution?  Why is it not correct behavior?
if speed > speedlimit:
    fine = 50 + 5*(speed-speedlimit)
    print("Your fine will be $", fine)
if speed > 90: 
    fine += 200
    print("Your fine will be $", fine)
if speed <= speedlimit:
    print("You are not speeding.")
    print("Continue safe driving practices.")

coinflip.py 3/6

[
top][prev][next]
# This program simulates a coin flip.
#
# by CSCI111

from random import *

coin = randint(1, 2)

if coin == 1:
    print("Heads!")
else:
    print("Tails!")
    

evenorodd.py 4/6

[
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.
# We are only doing one check of a condition.

#if remainder == 0:
#	print(x, "is even")
#else:
#	print(x, "is odd")

speedingticket2.py 5/6

[
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: "))

if speed <= speedlimit:
    print("Continue safe driving practices.") 
else: 
    # calculate the fine
    mphOver = speed - speedlimit
    fine = 50 + mphOver * 5

    if speed > 90:
        fine = fine + 200
        
    print("Your ticket is", fine)
    



speedingticket.py 6/6

[
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

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:
    # calculate the fine
    mphOver = speed - speedlimit
    fine = 50 + mphOver * 5

    if speed > 90:
        fine = fine + 200
        
    print("Your ticket is", fine)
else: 
    print("Continue safe driving practices.")




Generated by GNU Enscript 1.6.6.