Contents
- evenorodd.py
- fines.py
- no_constants.py
- pick4.nocomments.py
- pick4.py
- pick4winner2.py
- pick4winner.py
evenorodd.py 1/7
[top][prev][next]
# Demonstrates use of if statement
#
x = input("Enter a number: ")
remainder = x%2
if remainder == 0 :
print x, "is even"
if remainder == 1:
print x, "is odd"
fines.py 2/7
[top][prev][next]
# Calculate fines
# CS111
clockspeed = input("Enter the clocked speed: ")
speedlimit = input("Enter the speed limit: ")
if clockspeed <= speedlimit:
print "Continue safe driving practices"
else:
diff = clockspeed - speedlimit
fine = 50 + 5 * diff
if clockspeed > 90:
fine += 200
print "Slow down! You've been fined $" + str(fine) + "."
no_constants.py 3/7
[top][prev][next]
# Example program, no constants
# Program displays conversions of meters to kilometers, yards, and miles
print "Meters Kilometers Yards Miles "
print "-"*36
m=100
km=m*.001
yds=m*1.094
mi=m*.0006215
print "%6d %12.3f %7.1f %7.3f" % (m, km, yds, mi)
m=200
km=m*.001
yds=m*1.094
mi=m*.0006215
print "%6d %12.3f %7.1f %7.3f" % (m, km, yds, mi)
m=400
km=m*.001
yds=m*1.094
mi=m*.0006215
print "%6d %12.3f %7.1f %7.3f" % (m, km, yds, mi)
m=800
km=m*.001
yds=m*1.094
mi=m*.0006215
print "%6d %12.3f %7.1f %7.3f" % (m, km, yds, mi)
pick4.nocomments.py 4/7
[top][prev][next]
# Simulate the Pick 4 Lottery Game
#
#
#
# CS111
from random import randint
pick4num = ""
for x in xrange(3):
randchoice = randint(0, 9)
pick4num += str(randchoice)+"-"
randchoice = randint(0, 9)
pick4num += str(randchoice)
print "The Pick 4 Winner is", pick4num
pick4.py 5/7
[top][prev][next]
# Simulate the Pick 4 Lottery Game
# CS111
from random import randint
# the min and max range of values selected from ping-pong ball machines
MIN=0
MAX=9
# the number of choices to make
NUM_BALLS = 4
# the number that is chosen from the magic ping-pong ball machines
pick4num = ""
# Select the value for each 'ball'
# Accumulate the formatted Pick 4 number in a string
for x in xrange(NUM_BALLS-1):
randchoice = randint(MIN, MAX)
pick4num += str(randchoice)+"-"
randchoice = randint(MIN, MAX)
pick4num += str(randchoice)
print "The Pick 4 Winner is", pick4num
pick4winner2.py 6/7
[top][prev][next]
# Simulate the Pick 4 Lottery Game
# CS111
from random import randint
# the min and max range of values selected from ping-pong ball machines
MIN=0
MAX=9
# the number of choices to make
NUM_BALLS = 4
print "*"*50
print "This program simulates the Pick 4 VA Lottery game"
print "*"*50
print
userpick = input("Enter your Pick 4 number: ")
print "You picked", userpick
# the number that is chosen from the magic ping-pong ball machines
pick4str = ""
pick4num_10=0
pick4num_exp=0
# Select the value for each 'ball'
# Accumulate the formatted Pick 4 number in a string
for x in xrange(NUM_BALLS-1):
randchoice = randint(MIN, MAX)
pick4str += str(randchoice)+"-"
pick4num_10 = pick4num_10*10 + randchoice
pick4num_exp = pick4num_exp + randchoice *10 ** (NUM_BALLS-x-1)
print pick4num_10, pick4num_exp
randchoice = randint(MIN, MAX)
pick4str += str(randchoice)
pick4num_10 = pick4num_10*10+randchoice
pick4num_exp=pick4num_exp+randchoice
print "The Pick 4 Winner is", pick4str
print "Compare with generated numbers:", pick4num_10, pick4num_exp
if userpick == pick4num_10:
print "We have a winner!"
else:
print "Sorry, you lose!"
pick4winner.py 7/7
[top][prev][next]
# Simulate the Pick 4 Lottery Game
# Check if person picked a winner
# CS111
from random import randint
# the min and max range of values to choose from
MIN=0
MAX=9
# the number of choices to make
NUM_BALLS = 4
print "*"*50
print "This program simulates the Pick 4 VA Lottery game"
print "*"*50
print
# Get the user's pick
userpick = raw_input("Enter your Pick 4 number (in format #-#-#-#) ")
# the number that is chosen from the magic ping-pong ball machines
pick4num = ""
for x in xrange(NUM_BALLS-1):
randchoice = randint(MIN, MAX)
pick4num += str(randchoice)+"-"
randchoice = randint(MIN, MAX)
pick4num += str(randchoice)
print "The Pick 4 Winner is", pick4num
if userpick == pick4num:
print "We have a winner!"
else:
print "You lose! Good thing you didn't bet any money."
Generated by GNU enscript 1.6.4.