Contents

  1. descendSort.py
  2. file_read.py
  3. for_file_read.py
  4. swap.py
  5. using_readline.py
  6. wheeloffortune.py
  7. wheeloffortune.wfiles.py

descendSort.py 1/7

[
top][prev][next]
# Demonstrate passing lists to functions
# CSCI111

# this function tests the descend sort function
def main():
    # test descendSort3Nums
    list = [1,2,3]
    descendSort3Nums(list)
    print(list)

    list = [0, 5, -3]
    descendSort3Nums(list)
    print(list)
    
    list = [7,4,1]
    descendSort3Nums(list)
    print(list)
    
    list = [-1, -1, -3]
    descendSort3Nums(list)
    print(list)
    
    list = [-1, -5, -3]
    descendSort3Nums(list)
    print(list)

def descendSort3Nums(list3):
    """
    Parameter: a list containing three numbers
    Sorts the list in descending order
    Note: does not return anything, no output 
    (Consequence: can't test with testEqual function.)
    """
    if list3[1] > list3[0]:
        # swap 'em
        tmp = list3[0]
        list3[0] = list3[1]
        list3[1] = tmp

    if list3[2] > list3[1]:
        # swap 'em
        tmp = list3[1]
        list3[1] = list3[2]
        list3[2] = tmp
    
    if list3[1] > list3[0]:
        # swap 'em
        tmp = list3[0]
        list3[0] = list3[1]
        list3[1] = tmp


main()

file_read.py 2/7

[
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)

for_file_read.py 3/7

[
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:
    #line = line.strip()
    print(line)

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

swap.py 4/7

[
top][prev][next]
# Attempt at swapping two variables in a function
# FAIL!  Swapping within a function won't work. :(
# Why?  With immutable data types (like integers), the functions are
# passed *copies* of the 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) 


using_readline.py 5/7

[
top][prev][next]
# One way to use readline()
# 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
line = dataFile.readline()

while line != "":
    print(line)
    
    line = dataFile.readline()

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

wheeloffortune.py 6/7

[
top][prev][next]
# Wheel of Fortune
# the puzzle is hardcoded
# By Sara Sprenkle

PHRASE="Computational Thinkers".upper()
# 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() :
    # TODO: modify to keep track of previous guesses and let user
    # know if their guess was already made.
    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 7/7

[
top][prev][next]
# Wheel of Fortune
# gets the puzzles from a file
# 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}".format(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.