Contents
- file_read.py
- for_file_read.py
- swap.py
- wheeloffortune.wfiles.py
file_read.py 1/4
[top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle
FILENAME="data/alaska.dat"
# creates a new file object, opening the file in read mode
myFile = open(FILENAME, "r")
# read the file and put it into one string
contents = myFile.read()
# close the file when you're done reading the file
myFile.close()
# display the contents of the file
print(contents)
for_file_read.py 2/4
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents,
# by Sara Sprenkle
FILENAME="data/alaska.dat"
# creates a new file object, opening the file in "read" mode
dataFile = open(FILENAME, "r")
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
line = line.strip()
print(line)
# close the file with the method "close"
dataFile.close()
swap.py 3/4
[top][prev][next]
# Attempt at swapping two variables in a function
# FAIL! Swapping within a function won't work. :(
# Why? Only passing in *copies* of parameters, not the original variables
def swap( a, b):
tmp = a
a = b
b = tmp
print(a, b)
x = 5
y = 7
swap(x, y)
print("x =", x)
print("y =", y)
wheeloffortune.wfiles.py 4/4
[top][prev][next]
# Wheel of Fortune
# By Sara Sprenkle
PROMPT="Enter a letter or try to solve the puzzle: "
TITLE_WIDTH=50
# print out a nice header
print("*"*TITLE_WIDTH)
print("WHEEL".center(TITLE_WIDTH))
print("OF".center(TITLE_WIDTH))
print("FORTUNE!".center(TITLE_WIDTH))
print("*"*TITLE_WIDTH )
categoryNames = ["whatareyoudoing", "beforeandafter", "famous_pairs"]
for num in range(len(categoryNames)):
print("Press", (num+1), "for", categoryNames[num])
# input from user about which puzzle file to use
selection = eval(input("Which category do you choose? "))
# change categoryName to file names
puzzleFile = open("data/" + categoryNames[selection-1] + ".txt", "r")
puzzleid = 0
# each line in the puzzleFile is a puzzle
# go through them all ...
for puzzle in puzzleFile:
puzzle = puzzle.strip()
puzzle = puzzle.upper()
puzzleid+=1
# Display the current puzzle.
# All the alphabetical characters are displayed as underscores.
displayedPuzzle = ""
for char in puzzle:
if char.isalpha():
displayedPuzzle += "_"
else:
displayedPuzzle += char
print("Puzzle %d: %s" % (puzzleid, displayedPuzzle))
print()
# how many guesses it took to get it right
numGuesses = 1
guess = input(PROMPT)
while guess.lower() != puzzle.lower() :
if len(guess) == 1 and guess.isalpha():
numOccurences = puzzle.count(guess) + puzzle.count(guess.swapcase())
if numOccurences > 0:
print ("There are", numOccurences, guess + "'s", "in the phrase")
# fill in puzzle
updatedpuzzle=""
for pos in range(len(puzzle)):
if puzzle[pos] == guess or puzzle[pos] == guess.swapcase():
updatedpuzzle += puzzle[pos]
else:
updatedpuzzle += displayedPuzzle[pos]
displayedPuzzle = updatedpuzzle
print ("\nThe puzzle is", displayedPuzzle)
else:
print ("Sorry, there are no", guess + "'s", "in the word")
elif len(guess) != 1:
# assumes that the user tried to solve the puzzle but got it wrong.
print ("\tSorry, that is not correct.")
else:
print ("\tError: You must guess a letter.")
print()
guess = input(PROMPT)
numGuesses += 1
print ("Congratulations! You solved the puzzle in", numGuesses, "guesses")
puzzleFile.close()
print("We're out of puzzles! Thanks for playing!")
Generated by GNU Enscript 1.6.6.