Contents
- circleShiftAnim.py
- pick4winner_places.py
- search.py
- string_compare.py
- whilestr.py
circleShiftAnim.py 1/5
[top][prev][next]
# Move a circle to the position clicked by the user
# Repeat five times
# By CSCI 111
from graphics import *
from time import sleep
STEPS = 100
# create our canvas
win = GraphWin("Circle Shift", 300, 300)
# draw the circle in the middle of the screen
centerPt = Point(win.getWidth()/2, win.getHeight()/2)
circle = Circle( centerPt, 20 )
circle.setFill("blue")
circle.setWidth(0) # no outline
circle.draw(win)
# give the user some instructions
msg = "Click where you want to move the circle."
text = Text( Point(150, 290), msg)
text.draw(win)
for i in xrange(5):
# Update the instructions
text.setText(msg + "\nOnly " + str( 5-i) + " more to go")
# The user tells us where to click
newCenter = win.getMouse()
# figure out how far the circle needs to move
dx = newCenter.getX() - centerPt.getX()
dy = newCenter.getY() - centerPt.getY()
# move the circle
for step in xrange(STEPS):
circle.move( dx/float(STEPS) , dy/float(STEPS) )
sleep(.05)
# get the circle's new center point
centerPt = circle.getCenter()
text.setText("Click to exit")
win.getMouse()
pick4winner_places.py 2/5
[top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# By CS111
import random
# define constants that are easy to change so that our
# program is flexible
NUM_BALLS = 4
MIN_VALUE = 0
MAX_VALUE = 9
# Create the format for the number
numFormat = ""
for num in xrange(NUM_BALLS-1):
numFormat += "#-"
numFormat += "#"
pickedNum = raw_input("What is your pick? (" + 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
count = 0
# Don't want to count hyphens, so look at every other
# position, starting at 0
for i in xrange(0, len(pickedNum), 2):
if pickedNum[i] == winningNum[i]:
count+=1
if pickedNum == winningNum:
print "Congratulations! You're the big winner!!!!!!"
elif count > 1:
print "You got", count, "numbers correct."
elif count == 1:
print "You got 1 number correct."
else:
print "Please waste more money and play again."
search.py 3/5
[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 4/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 "-------------------------"
if str1 < str2 :
print "Alphabetically, ", str1, "comes before", str2
else :
print "Alphabetically, ", str2, "comes before", str1
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)
i=0
while i < len(str) :
print "%5d %9s" % (i, str[i])
i+=1
print "\nAlternatively, using a for loop...\n"
print header1, header2
print "-"*len(header1), "-"*len(header2)
for pos in xrange(len(str)):
print "%5d %9s" % (pos, str[pos])
Generated by GNU enscript 1.6.4.