Contents
- avgData.py
- file_read2.py
- file_read3.py
- file_read.py
- file_search2.py
- file_search.py
- file_write.py
- wheeloffortune.wfiles.py
avgData.py 1/8
[top][prev][next]
# Computes the average high temperature from a file that contains the daily
# high temperatures for last year at one location.
# CSCI 111, 03.07.2011
DATAFILE="data/alaska.dat"
tempFile = file(DATAFILE, "r")
totalValue = 0
totalDays = 0
# Read through the file
for line in tempFile:
# convert line into a float
temperature = float(line)
# update the accumulators
totalValue += temperature
totalDays += 1
tempFile.close()
average = totalValue/totalDays
print "The average temperature for", totalDays , "days is %.2f" % average
file_read2.py 2/8
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
# reads in the file, line-by-line and displays the content of the file
line = dataFile.readline()
while line != "":
print line,
line = dataFile.readline()
# close the file with the method "close"
dataFile.close()
file_read3.py 3/8
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents,
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
print line,
# close the file with the method "close"
dataFile.close()
file_read.py 4/8
[top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle
FILENAME="data/years.dat"
# creates a new file object, opening the file in read mode
myFile = file(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,
file_search2.py 5/8
[top][prev][next]
# Given a file and a term to search for,
# find which lines the term is on and the
# total number of lines that contained that term
# CSCI 111, 3/7/2011
FILENAME="data/years2.dat"
roster = file(FILENAME, "r")
term = "FY"
count = 0
lineNum = 1
# check every line in the file and see where term occurs
for line in roster:
line = line.strip()
if line[0] != "#":
if term in line:
count += 1
print lineNum, line
lineNum += 1
roster.close()
file_search.py 6/8
[top][prev][next]
# Given a file and a term to search for,
# find which lines the term is on and the
# total number of lines that contained that term
# CSCI 111, 3/7/2011
FILENAME="data/years.dat"
roster = file(FILENAME, "r")
term = "FY"
count = 0
lineNum = 1
# check every line in the file and see where term occurs
for line in roster:
line = line.strip()
if term in line:
count += 1
print lineNum, line
lineNum += 1
roster.close()
file_write.py 7/8
[top][prev][next]
# Writes content from a user to a file
# by Sara Sprenkle
PROMPT = "What do you want to add to the file? (Nothing will exit): "
outfilename = raw_input("What is the name of your output file? ")
# create a new file object, in "write" mode
dataFile = file(outfilename, "w")
while True:
userinput = raw_input(PROMPT)
if userinput == "" :
break
# write the user's input to the file
dataFile.write(userinput)
# write a newline after each input from the user
dataFile.write("\n")
# close the file with the method "close"
dataFile.close()
wheeloffortune.wfiles.py 8/8
[top][prev][next]
# 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"
print
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
# Not completed ...
#
def tossUp(puzzle):
length = len(puzzle)
#for num in xrange(length):
# fillInRandomLetter(origPuzzle, displayPuzzle)
#
#
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.