Contents

  1. pick4num_places.py
  2. search.py
  3. string_compare.py
  4. whilestr.py

pick4num_places.py 1/4

[
top][prev][next]
# Simulate the Pick 4 Lottery Game
# Check if person picked a winner
# MODIFIED: Checks how many numbers are in the right place.
# 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

# Calculate how many numbers the user got in the correct position
num_correct = 0

for pos in xrange( len(pick4num) ):
    if pick4num[pos] == userpick[pos]:
        num_correct+=1

# Display results
print
if userpick == pick4num:
    print "We have a winner!"
elif num_correct > 0:
    print "Consolation prize: You got", num_correct, "numbers right!"
else:
    print "You lose!  Good thing you didn't bet any money."

search.py 2/4

[
top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
#

# 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/4

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

if str1 < str2 :
    print "Alphabetically,", str1, "comes before", str2
else :
    print "Alphabetically,", str2, "comes before", str1

whilestr.py 4/4

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

i=0

while i < len(str) :
    print "%5d %9s" % (i, str[i])
    i+=1


Generated by GNU enscript 1.6.4.