Contents
- birthyear.py
- eval_cond.py
- evenorodd.py
- fine.py
- pick4winner.py
birthyear.py 1/5
[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
eval_cond.py 2/5
[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 = (y<4) or (z<3)
print d
d = not d
print b, c, d
evenorodd.py 3/5
[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 4/5
[top][prev][next]
# This program determines if someone is speeding and,
# if they are speeding, computes the fine.
# CS111, 01.31.2011
EXCESSIVE_SPEED=90
print "This program "
# define speed limit as a constant
speed_limit = input("Enter the speed limit: ")
clockspeed= 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 $%d." % fine
pick4winner.py 5/5
[top][prev][next]
# Simulate the VA Pick4 Lottery game
# CS111, 1.26.2011
import random
NUM_CHOICES=4
NUM_START_RANGE=0
NUM_STOP_RANGE=9
print "This program will generate the winning lottery number for"
print "the VA Pick4 Lottery game."
print
winningNum = ""
for x in xrange(NUM_CHOICES-1):
randomNum = random.randint(NUM_START_RANGE, NUM_STOP_RANGE)
#print randomNum, "-",
winningNum = winningNum + str(randomNum) + "-"
# equivalent:
# winningNum += str(randomNum) + "-"
randomNum = random.randint(NUM_START_RANGE, NUM_STOP_RANGE)
#print randomNum
winningNum = winningNum + str(randomNum)
print "The winning number is", winningNum
Generated by GNU enscript 1.6.4.