Contents

  1. lab2.4.py
  2. lab2.4.withfunctions.py
  3. menu.py
  4. menu_withfunctions.py
  5. menu_withoutfunc.py
  6. oldmac.py
  7. string_methods.py
  8. swap.py
  9. wheeloffortune.py

lab2.4.py 1/9

[
top][prev][next]
# lab2.4.py
#
# Displays equivalents for Olympic running distances in
# meters, kilometers, yards, miles

# conversion constants
M2KILO=.001
M2YARD=1.094
M2MILE=.0006215

# distances in meters
dist1=100
dist2=200
dist3=400
dist4=800

# print column headings
print "Meters  Kilometers  Yards  Miles"
print "-"*32

# calculate conversions, print data for conversion table
kilo1=dist1*M2KILO
yard1=dist1*M2YARD
mile1=dist1*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist1, kilo1, yard1, mile1)

kilo2=dist2*M2KILO
yard2=dist2*M2YARD
mile2=dist2*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist2, kilo2, yard2, mile2)

kilo3=dist3*M2KILO
yard3=dist3*M2YARD
mile3=dist3*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist3, kilo3, yard3, mile3)

kilo4=dist4*M2KILO
yard4=dist4*M2YARD
mile4=dist4*M2MILE
print "%6.0i %11.3f %6.1f %6.3f" % (dist4, kilo4, yard4, mile4)


lab2.4.withfunctions.py 2/9

[
top][prev][next]
# - Displays equivalents for Olympic running distances in
# meters, kilometers, yards, miles
# - Uses a function for converting from meters to miles.

# converts meters to miles
def metersToMiles(meters):
    METERS_TO_MILES=.0006215
    result = meters * METERS_TO_MILES
    return result

# conversion constants
M2KILO=.001
M2YARD=1.094

# distances in meters
dist1=100
dist2=200
dist3=400
dist4=800

# print column headings
print "Meters  Kilometers  Yards  Miles"
print "-"*32

# calculate conversions, print data for conversion table
kilo1=dist1*M2KILO
yard1=dist1*M2YARD
mile1=metersToMiles(dist1)
print "%6.0i %11.3f %6.1f %6.3f" % (dist1, kilo1, yard1, mile1)

kilo2=dist2*M2KILO
yard2=dist2*M2YARD
mile2=metersToMiles(dist2)
print "%6.0i %11.3f %6.1f %6.3f" % (dist2, kilo2, yard2, mile2)

kilo3=dist3*M2KILO
yard3=dist3*M2YARD
mile3=metersToMiles(dist3)
print "%6.0i %11.3f %6.1f %6.3f" % (dist3, kilo3, yard3, mile3)

kilo4=dist4*M2KILO
yard4=dist4*M2YARD
mile4=metersToMiles(dist4)
print "%6.0i %11.3f %6.1f %6.3f" % (dist4, kilo4, yard4, mile4)


menu.py 3/9

[
top][prev][next]
# Module that contains useful menu functions
# Sara Sprenkle

# Displays a formatted welcome message, for a 
# given program named "name"
def printWelcomeScreen(name):
    welcome = "Welcome to " + name + "!"
    length = len(welcome)
    print length*"-"
    print welcome
    print length*"-"


# Display a user's menu options
def printMenu():
    print "You have some options for what to do: "
    print "Enter an 'F' to find a song"
    print "Enter an 'S' to sort by Song title"
    print "Enter an 'A' to sort by Album"
    print "Enter an 'R' to sort by aRtist name"
    print "Enter an 'H' to list your options again"
    print "Enter a 'Q' to quit"

menu_withfunctions.py 4/9

[
top][prev][next]
# Using functions from menu module
# by Sara Sprenkle

import menu

STOP_OPTION = 'Q'

menu.printWelcomeScreen("Music Manager")
menu.printMenu()

menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()

while menuChoice != STOP_OPTION :
    print "Do something appropriate for", menuChoice
    menu.printMenu()
    menuChoice = raw_input("Which option do you choose? ")
    menuChoice = menuChoice.upper()

menu_withoutfunc.py 5/9

[
top][prev][next]
# What code would look like, as we've done before
# (without functions)
# by Sara Sprenkle

STOP_OPTION = 'Q'

name = "Music Manager"

welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"
print welcome
print length*"-"

print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
    
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()

while menuChoice != STOP_OPTION :
    print "Do something appropriate for", menuChoice
    print "You have some options for what to do: "
    print "Enter an 'F' to find a song"
    print "Enter an 'S' to sort by Song title"
    print "Enter an 'A' to sort by Album"
    print "Enter an 'R' to sort by aRtist name"
    print "Enter an 'H' to list your options again"
    print "Enter a 'Q' to quit"
    menuChoice = raw_input("Which option do you choose? ")
    menuChoice = menuChoice.upper()

oldmac.py 6/9

[
top][prev][next]
# Print out verses of the song Old MacDonald
# Sara Sprenkle
#

BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"

def main():
    # call the verse function to print out a verse
    printVerse("dog", "ruff")
    printVerse("duck", "quack")
    
    animal_type = "cow"
    animal_sound = "moo"
    
    printVerse(animal_type, animal_sound)
    

# QUESTION: What if called function as
# printVerse("ruff", "dog")


# prints a verse of Old MacDonald, plugging in the animal and sound
# parameters (which are strings), as appropriate.
def printVerse(animal, sound):
    print BEGIN_END + EIEIO
    print "And on that farm he had a " + animal + EIEIO
    print "With a " + sound + ", " + sound + " here"
    print "And a " + sound + ", " + sound + " there"
    print "Here a", sound
    print "There a", sound
    print "Everywhere a " + sound + ", " + sound
    print BEGIN_END + EIEIO
    print

main()

#if __name__ == "__main__":
#    main()

string_methods.py 7/9

[
top][prev][next]
# Do stuff to strings, using methods
# by Sara Sprenkle

string = raw_input("Enter a sentence to mangle: ")

length = len(string)

print "*", string.center(int(length*1.5)), "*"

strUpper=string.upper()

print strUpper
print string.lower()

# Answer before running...
print "Did string change?: ", string



#print string.replace("a", "o")
#print "Did string change?: ", string

swap.py 8/9

[
top][prev][next]
# Showing pass-by-value of functions with swap function
# Sara Sprenkle

# Attempt to swap two values
def swap( x, y ):
    temp = x
    x = y
    y = temp


x = 5
y = 10

print "x is", x
print "y is", y

swap(x,y)

#temp = x
#x = y
#y = temp

print "x is", x
print "y is", y

wheeloffortune.py 9/9

[
top][prev][next]
# Wheel of Fortune
#

PHRASE="Welcome back, Kotter"
# 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 characters (except for spaces)
# are displayed as underscores
puzzle = ""
for char in PHRASE:
    #if char != " ":
    if char.isalpha():
        puzzle += "_"
    else:
        puzzle += char

print "The puzzle:", puzzle

print
print "Let's get started!"

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

guess = raw_input(PROMPT)

# Check if the user's guess matches the phrase
while guess.upper() != PHRASE.upper() :

    # handle a letter guess
    if len(guess) == 1:
        numOccurences = PHRASE.count(guess) + PHRASE.count(guess.swapcase())
        if numOccurences > 0:
            print "There are", numOccurences, guess + "'s", "in the phrase"
            # fill in puzzle with guessed letter
            updatedpuzzle=""
            for pos in xrange(len(PHRASE)):
                if PHRASE[pos] == guess or PHRASE[pos] == guess.swapcase():
                    updatedpuzzle += PHRASE[pos]
                else:
                    updatedpuzzle += puzzle[pos]
            puzzle = updatedpuzzle
            
            print "\nThe puzzle is", puzzle
        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"

Generated by GNU enscript 1.6.4.