Contents
- pick4winner_places.py
- search.py
- string_compare.py
- string_methods.py
- whilestr.py
pick4winner_places.py 1/5
[top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# Modified to count number of correctly chosen numbers
# By CSCI111
import random
# define constants that are easy to change so that our
# program is flexible
NUM_BALLS = 4
MIN_VALUE = 0
MAX_VALUE = 2
# Create the format for the number
numFormat = ""
for num in xrange(NUM_BALLS-1):
numFormat += "#-"
numFormat += "#"
pickedNum = raw_input("What is your pick? (Format: " + numFormat + ") ")
while len(pickedNum) != (NUM_BALLS*2-1):
print "Incorrectly formatted number."
pickedNum = raw_input("What is your pick? (Format: " + numFormat + ") ")
# Accumulator variable for the winning number
winningNum = ""
for num in xrange(NUM_BALLS-1):
# generate a random number (simulating the ping pong ball machine
# spitting out a random ping pong ball)
randomNum = random.randint(MIN_VALUE, MAX_VALUE)
# add the random number to the winning number
winningNum = winningNum + str(randomNum)
# add the hyphen to the winning number
winningNum += "-"
# add the final random number to the winning number
winningNum += str(random.randint(MIN_VALUE, MAX_VALUE))
# display the winning number
print "The winning number is", winningNum
if pickedNum == winningNum:
print "Congratulations! You're the big winner!!!!!!"
else:
# determine the number of correct numbers in the pickedNum
correctNumbers=0
for x in xrange(0, len(pickedNum), 2):
if pickedNum[x] == winningNum[x]:
correctNumbers += 1
print "You got", correctNumbers, "right!"
if correctNumbers > 1:
print "You're getting close!"
print "Please waste more money and play again."
search.py 2/5
[top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
# Sara Sprenkle
# QUESTION: Why is this a constant?
PYTHON_EXT = ".py"
filename = raw_input("Enter a filename: ")
if filename[-(len(PYTHON_EXT)):] == PYTHON_EXT:
print "That's a name for Python script"
if PYTHON_EXT in filename:
print "That filename contains", PYTHON_EXT
# QUESTION: SHOULD THIS BE AN IF/ELIF ?
string_compare.py 3/5
[top][prev][next]
# Program compares two strings
# by Sara Sprenkle
str1 = raw_input("Enter a string to compare: ")
str2 = raw_input("Compare '" + str1 + "' with what string? ")
print "-" * 40
if str1 < str2 :
print "Alphabetically,", str1, "comes before", str2
else :
print "Alphabetically,", str2, "comes before", str1
string_methods.py 4/5
[top][prev][next]
# Manipulate strings, using methods
# by Sara Sprenkle
sentence = raw_input("Enter a sentence to mangle: ")
length = len(sentence)
# Question: What does the statement below do?
print "*", sentence.center(int(length*1.5)), "*"
print "Uppercase: ", sentence.upper()
print
print "Lowercase: ", sentence.lower()
print
# Answer before running...
print "Did sentence change?: ", sentence
whilestr.py 5/5
[top][prev][next]
# Iterating through a string
# by Sara Sprenkle
print
str = raw_input("Enter a string to iterate through: ")
print
header1 = "index"
header2 = "character"
print header1, header2
print "-"*len(header1), "-"*len(header2)
for pos in xrange(len(str)):
print "%5d %9s" % (pos, str[pos])
print "\nAlternatively, using a while loop...\n"
print header1, header2
print "-"*len(header1), "-"*len(header2)
i=0
while i < len(str) :
print "%5d %9s" % (i, str[i])
i+=1
Generated by GNU enscript 1.6.4.