Contents

  1. file_read2.py
  2. file_read.py
  3. file_read.py~
  4. wheeloffortune.py
  5. wheeloffortune.wfiles.py

file_read2.py 1/5

[
top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents,
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# 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:
    # remove the newline character from the line
    line = line[:-1]
    # didn't use rstrip method in case want to preserve trailing
    # spaces from the original file
    print(line)

# close the file with the method "close"
dataFile.close()

file_read.py 2/5

[
top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# 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, end='')


# If I wanted to break the contents into separate lines, I could do
# the following:
# lines = contents.split("\n")
# what does the above line do?

file_read.py~ 3/5

[
top][prev][next]
# Opens a file, reads it, and prints out its contents.
# by Sara Sprenkle

FILENAME="data/famous_pairs.txt"

# 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, end='')

wheeloffortune.py 4/5

[
top][prev][next]
# Wheel of Fortune
# By Sara Sprenkle

PHRASE="FOUR PUZZLES FROM CYBERSPACE"
# PHRASE = ""
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 )

# Display the current puzzle.  
# All the alphabetical characters are displayed as underscores.
displayedPuzzle = ""
for char in PHRASE:
    if char.isalpha():
        displayedPuzzle += "_"
    else:
        displayedPuzzle += char

print ("The puzzle:", displayedPuzzle)

print()
print ("Let's get started!")

# how many guesses it took to get it right
numGuesses = 1

guess = input(PROMPT)

while guess.lower() != PHRASE.lower() :
    if len(guess) == 1 and guess.isalpha(): 
        numOccurences = PHRASE.count(guess) + PHRASE.count(guess.swapcase())
        if numOccurences > 0:
            print ("There are", numOccurences, guess + "'s", "in the phrase")
            # fill in puzzle
            updatedpuzzle=""
            for pos in range(len(PHRASE)):
                if PHRASE[pos] == guess or PHRASE[pos] == guess.swapcase():
                    updatedpuzzle += PHRASE[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")



wheeloffortune.wfiles.py 5/5

[
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 = ["movies", "songs", "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()
    
    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.4.