Contents
- evenorodd.py
- mega_millions.py
- pick4.py
- program_after.py
- program_before.py
- speedingticket.py
evenorodd.py 1/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, could use an "else" statement
# instead of the second if statement
mega_millions.py 2/6
[top][prev][next]
# Simulate the VA Mega Millions Lottery game
# CS111, 1.28.2011
import random
print "This program will generate the winning lottery number for"
print "the Mega Millions Lottery game."
print
NUM_CHOICES=5
NUM_START_RANGE=1
NUM_STOP_RANGE=56
winningNum = ""
for x in xrange(NUM_CHOICES-1):
randomNum = random.randint(NUM_START_RANGE, NUM_STOP_RANGE)
winningNum = winningNum + str(randomNum) + "-"
# equivalent:
# winningNum += str(randomNum) + "-"
randomNum = random.randint(NUM_START_RANGE, NUM_STOP_RANGE)
winningNum = winningNum + str(randomNum)
print "The winning number is", winningNum
pick4.py 3/6
[top][prev][next]
# Simulate the VA Pick4 Lottery game
# CS111, 1.26.2011
import random
print "This program will generate the winning lottery number for"
print "the VA Pick4 Lottery game."
print
winningNum = ""
for x in xrange(3):
randomNum = random.randint(0,9)
#print randomNum, "-",
winningNum = winningNum + str(randomNum) + "-"
# equivalent:
# winningNum += str(randomNum) + "-"
randomNum = random.randint(0,9)
#print randomNum
winningNum = winningNum + str(randomNum)
print "The winning number is", winningNum
program_after.py 4/6
[top][prev][next]
# This program converts kilometers to miles.
# Revised by CS111 class, 01.28.2011
print "This program converts kilometers to miles."
KM_TO_MILES=.62
# get input from user
kilometers = input("Enter the number of kilometers: ")
# convert kilometers to miles
miles = kilometers * KM_TO_MILES
# display output to user
print kilometers, "km is", miles, "miles."
program_before.py 5/6
[top][prev][next]
k = input("Enter the number: ")
x = k * .62
print x
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.
# Our program
# 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.")
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.