Contents

  1. fencepost_problem.py
  2. pick4.py
  3. random_test.py
  4. tictactoe.py

fencepost_problem.py 1/4

[
top][prev][next]
# Solution to the Fence Post Problem
# by Sara Sprenkle 09.24.2007

# Note that you have one more fence post (|) than you have beams (-).
# So, you must execute the for loop one less time and then add the
# last fencepost.

num_fp = input("Enter the length of fence you want, in fence posts: ")

fence_str = ""

for fp in xrange(num_fp - 1):
    fence_str += "|-"

fence_str += "|"

print fence_str


# Alternative solution:

fence_str = "|-" * (num_fp -1)
fence_str += "|"
print fence_str

pick4.py 2/4

[
top][prev][next]
# Simulate the Pick 4 Lottery Game
# 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


# 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

random_test.py 3/4

[
top][prev][next]
# Demonstrating random module
# by Sara Sprenkle

import random

# Demonstrates that it's a pseudo-random number generator
# If using the same seed, then gets the same list of numbers
#random.seed(1)

for x in xrange(10):
    print random.random()


tictactoe.py 4/4

[
top][prev][next]
# Display a tic-tac-toe board
# CS111

for row in xrange(2):
    #for col in xrange(2):
    #    print "  |",
    #print
    print "  |"*2
    print "-"*9
#for col in xrange(2):
#    print "  |",
print "  |"*2

Generated by GNU enscript 1.6.4.