wheeloffortune.py
# Wheel of Fortune
# - Updated to use a file of puzzles ...
# - Added functions to improve readability
# Sara Sprenkle
def main():
PROMPT="Enter a letter or try to solve the puzzle: "
TITLE_WIDTH=50
categories = ["oscars", "grammy_noms", "famous_pairs"]
printHeader(TITLE_WIDTH)
print
print "Let's get started!"
puzzleFile = userPuzzleSelection(categories)
puzzleid = 0
# each line in the puzzleFile is a puzzle
# go through them all ...
for puzzle in puzzleFile:
puzzle = puzzle.strip()
puzzleid+=1
# Display the current puzzle. All the characters (except for spaces)
# are displayed as underscores
displayedPuzzle = displayPuzzle(puzzle)
print "Puzzle %d: %s" % (puzzleid, displayedPuzzle)
# how many guesses it took the user to get it right
numGuesses = 1
oldGuesses = []
guess = raw_input(PROMPT)
# Check if the user's guess matches the phrase
while guess.upper() != puzzle.upper() :
#print oldGuesses
if guess in oldGuesses:
print "You guessed", guess, "already."
else:
oldGuesses.append(guess)
# handle a letter guess
if len(guess) == 1:
numOccurences = puzzle.count(guess) + puzzle.count(guess.swapcase())
if numOccurences > 0:
print "There are", numOccurences, guess + "'s", "in the phrase"
# fill in puzzle with guessed letter
displayedPuzzle = updateDisplayedPuzzle(displayedPuzzle, guess, puzzle)
print "\nThe puzzle is", displayedPuzzle
else:
print "Sorry, there are no", guess + "'s", "in the word"
# handle an incorrect guess of the phrase
else:
print "You guessed incorrectly."
print
guess = raw_input(PROMPT)
numGuesses += 1
print "Congratulations! You solved the puzzle in", numGuesses, "guesses"
puzzleFile.close()
# display a nice header for this program, with the given width
def printHeader(width):
# print out a nice header
print "*"*width
print "WHEEL".center(width)
print "OF".center(width)
print "FORTUNE!".center(width)
print "*"*width
# returns a string that represents the puzzle, replacing characters with
# underscores.
def displayPuzzle(puzzle):
displayedPuzzle = ""
for char in puzzle:
if char.isalpha():
displayedPuzzle += "_"
else:
displayedPuzzle += char
return displayedPuzzle
# Returns the updated puzzle to display, given the current displayed puzzle,
# the user's guess, and the puzzle
def updateDisplayedPuzzle(displayedPuzzle, guess, puzzle):
updatedpuzzle=""
for pos in xrange(len(puzzle)):
if puzzle[pos] == guess or puzzle[pos] == guess.swapcase():
updatedpuzzle += puzzle[pos]
else:
updatedpuzzle += displayedPuzzle[pos]
return updatedpuzzle
#
#
def userPuzzleSelection(categoryNames):
#print "Select your category from", categoryNames
for num in xrange(len(categoryNames)):
print "Press", num, "for", categoryNames[num]
# input from user
selection = input("Which category do you choose? ")
# change categoryName to file names
puzzleFile = file("data/" + categoryNames[selection] + ".txt", "r")
return puzzleFile
main()
Generated by GNU enscript 1.6.4.