Contents

  1. birthyear.py
  2. coinflip.py
  3. eval_cond.py
  4. evenorodd.py
  5. fine.py
  6. grade_elif.py
  7. grade.py
  8. speedingticket.py

birthyear.py 1/8

[
top][prev][next]
# This program calculates your birthyear, 
# given your age and the current year.
# Sara Sprenkle

import sys 

print "This program determines your birth year"
print "given your age and current year."
print
age = input("Enter your age >> ")
if age > 120:
	print "Don't be ridiculous.  You can't be that old!"
	sys.exit(1)

# input is reasonable ...
currentYear = input("Enter the current year >> ")
birthyear = currentYear - age
print
print "You were either born in", birthyear, "or",
print birthyear-1


coinflip.py 2/8

[
top][prev][next]
# This program demonstrates the use of the pseudo random number
# generator to simulate coin flips
# by Sara Sprenkle

from random import randint

HEADS=0
TAILS=1

# flip the coin
if randint(0,1) == HEADS:
    print("heads")
else:
    print("tails")


eval_cond.py 3/8

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


evenorodd.py 4/8

[
top][prev][next]
# This program determines whether a number is even or odd
# Sara Sprenkle

x = input("Enter a number: ")

remainder = x%2

if remainder == 0:
	print x, "is even"

if remainder == 1:
	print x, "is odd"

fine.py 5/8

[
top][prev][next]
# This program determines if someone is speeding and, 
# if they are speeding, computes the fine.
# Alternative

EXCESSIVE_SPEED=90

print("This program computes a fine, given the speed limit and the clocked speed")

# define speed limit as a constant
speed_limit = eval(input("Enter the speed limit: "))
clockspeed= eval(input("Enter the clocked speed: "))

# determine if they're speeding
if clockspeed <= speed_limit:
    print("No fine!  Good job!")
else: 
    # figure out how much they're speeding
    diff = clockspeed - speed_limit
    # figure out the fine to pay
    # base of 
    fine = 50+5*diff
    # determine if they were speeding excessively
    if clockspeed > EXCESSIVE_SPEED:
        fine += 200
    print("Slow down!  Your speed is too fast.")
    print("Your fine is $", fine)

grade_elif.py 6/8

[
top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111

import sys

numericGrade = eval(input("Enter the numeric grade: "))

# One way to do error handling.  We discussed other approaches
if numericGrade < 0 or numericGrade > 100:
    print("Grade must be between 0 and 100, inclusive.")
    sys.exit(1)

if numericGrade >= 90:
    letter_grade = "A"
elif numericGrade >= 80:
    letter_grade = "B"
elif numericGrade >=70:
    letter_grade = "C"
elif numericGrade >= 60:
    letter_grade = "D"
else:
    letter_grade = "F"
                
print("Your letter grade is", letter_grade)


grade.py 7/8

[
top][prev][next]
# Compute the letter grade, based on the numeric grade
# CSCI 111

numericGrade = eval(input("Enter the numeric grade: "))

if numericGrade >= 90:
    letter_grade = "A"
else:
    if numericGrade >= 80:
        letter_grade = "B"
    else:
        if numericGrade >=70:
            letter_grade = "C"
        else:
            if numericGrade >= 60:
                letter_grade = "D"
            else:
                letter_grade = "F"
                
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.


speedingticket.py 8/8

[
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, 1/27/2012

print("This program determines whether you were speeding and your fine,")
print("if appropriate.\n")

speed_limit = eval(input("What is the speed limit? "))
clocked_speed = eval(input("What was the speed? "))

# determine if the user was speeding
if clocked_speed <= speed_limit:
    print("The clocked speed is under the speed limit.")
    print("No fine.  Continue safe driving.")
else:
    # reckless driving
    if clocked_speed > 90:
        # compute fine
        fine = 50+5*(clocked_speed - speed_limit) + 200
    else:
        # "just" speeding--not reckless; compute fine
        fine = 50+5*(clocked_speed - speed_limit)
    print("You were speeding.  Your fine is", fine)


Generated by GNU enscript 1.6.4.