Contents

  1. birthyear.py
  2. coinflip.py
  3. eval_cond.py
  4. grade_elif.py
  5. grade.py
  6. speedingticket2.py
  7. speedingticket.py

birthyear.py 1/7

[
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 = eval(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 = eval(input("Enter the current year: "))
birthyear = currentYear Ð age
print()
print("You were either born in", birthyear, end=ÔÕ)
print("or", birthyear-1)

coinflip.py 2/7

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

[
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
# I added parentheses for readability/emphasis
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)


grade_elif.py 4/7

[
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.py 5/7

[
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"
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 6/7

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

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