Contents
- coinflip.py
- consecutiveHeads2.py
- consecutiveHeads.py
- tictactoe2.py
- tictactoe.py
coinflip.py 1/5
[top][prev][next]
# This program demonstrates the use of the pseudo random number
# generator to simulate coin flips
# by Sara Sprenkle
from random import randint
HEADS=0
TAILS=1
# flip the coin
if randint(0,1) == HEADS:
print "heads"
else:
print "tails"
consecutiveHeads2.py 2/5
[top][prev][next]
# Count how many times it takes to get 3 consecutive heads
# By CSCI111, 02.07.2011
from random import randint
HEADS=0
TAILS=1
# accumulator of the number of times we flipped heads
conHeads = 0
# accumulator of the number of times we flipped the coin
numFlips = 0
# infinite loop ...
while True:
# flip the coin
if randint(0,1) == HEADS:
print "heads"
conHeads += 1
else:
print "tails"
# reset the number of consecutive heads to 0 because we just flipped tails!
conHeads = 0
numFlips += 1
# Tells the program when to stop, i.e., when we have flipped 3 consecutive heads
if conHeads == 3:
print "It took", numFlips, "times to get 3 consecutive heads."
break
consecutiveHeads.py 3/5
[top][prev][next]
# Count how many times it takes to get 3 consecutive heads
# By CSCI111, 02.07.2011
from random import randint
HEADS=0
TAILS=1
# accumulator of the number of times we flipped heads
conHeads = 0
# accumulator of the number of times we flipped the coin
numFlips = 0
# Keep flipping coins if we have less than 3 consecutive heads
while conHeads < 3:
# flip the coin
if randint(0,1) == HEADS:
print "heads"
conHeads += 1
else:
print "tails"
# reset the number of consecutive heads to 0 because we just flipped tails!
conHeads = 0
numFlips += 1
print "It took", numFlips, "times to get 3 consecutive heads."
tictactoe2.py 4/5
[top][prev][next]
#Create full-size tic-tac-toe board
# Uses clone and move to simplify program
# By CSCI111
from graphics import *
win = GraphWin("Tic-Tac-Toe", 300, 300)
width=win.getWidth()
height=win.getHeight()
# create and draw the vertical lines
vertLine1 = Line(Point(width/3.0,0), Point(width/3.0, width))
vertLine1.setWidth(4)
vertLine1.setOutline("purple")
vertLine2=vertLine1.clone() # maintains the properties of the line
vertLine2.move(width/3.0,0)
vertLine1.draw(win)
vertLine2.draw(win)
# create and draw the horizontal lines
horizLine1 = Line(Point(0,height/3.0), Point(width,height/3.0))
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine2 = horizLine1.clone()
horizLine2.move(0, height/3.0)
horizLine1.draw(win)
horizLine2.draw(win)
win.getMouse()
tictactoe.py 5/5
[top][prev][next]
#Create full-size tic-tac-toe board
# By CSCI111
from graphics import *
win = GraphWin("Tic-Tac-Toe", 300, 300)
width=win.getWidth()
height=win.getHeight()
# create and draw the vertical lines
vertLine1 = Line(Point(width/3.0,0), Point(width/3.0, height))
vertLine1.setWidth(3)
vertLine1.setOutline("purple")
vertLine2 = Line(Point(2*width/3.0,0), Point(2*width/3.0, height))
vertLine2.setWidth(3)
vertLine2.setOutline("purple")
vertLine1.draw(win)
vertLine2.draw(win)
# create and draw the horizontal lines
horizLine1 = Line(Point(0,height/3.0), Point(width,height/3.0))
horizLine1.setWidth(3)
horizLine1.setOutline("purple")
horizLine2 = Line(Point(0,2*height/3.0), Point(width,2*height/3.0))
horizLine2.setWidth(3)
horizLine2.setOutline("purple")
horizLine1.draw(win)
horizLine2.draw(win)
win.getMouse()
Generated by GNU enscript 1.6.4.