Contents
- menu.py
- menu_withfunctions.py
- mystery.py
- non_function_vars.py
- oldmac.py
- practice1.py
- practice2.py
- scope.py
- swap.py
menu.py 1/9
[top][prev][next]
# A module of useful menu functions, data
STOP_OPTION = 'Q'
def printWelcomeScreen(name):
welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"
print welcome
print length*"-"
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 2/9
[top][prev][next]
# Using functions from menu module
# by Sara Sprenkle
from menu import *
printWelcomeScreen("MusicManager")
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
while menuChoice != STOP_OPTION :
printMenu()
menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()
mystery.py 3/9
[top][prev][next]
# Mystery Program
# Used to demonstrate variable lifetimes and scope
def main():
x = 10
sum = sumEvens( x )
print "The sum of even #s up to", x, "is", sum
def sumEvens(limit):
total = 0
for x in xrange(0, limit, 2):
total += x
return total
main()
non_function_vars.py 4/9
[top][prev][next]
# Using variables that aren't part of any function
# by Sara Sprenkle
# create variables that aren't part of any function
non_func = 2
non_func_string = "aardvark"
def main():
func()
print non_func
print non_func_string
def func():
print "In func: nf =", non_func
print "In func: nfs =", non_func_string
# Question: what happens when we try to assign the variables that
# aren't part of a function a value?
# Answer:
main()
non_func = 6
non_func_string = "dog"
print non_func
print non_func_string
oldmac.py 5/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 happens if main 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
if __name__ == '__main__':
main()
practice1.py 6/9
[top][prev][next]
# Exercising your knowledge of variable scope.
#
def main():
num = input("Enter a number to be squared: ")
squared = square(num)
print "The square is: ", squared
def square(n):
return n * n
main()
practice2.py 7/9
[top][prev][next]
# Exercising your knowledge of variable scope.
def main():
num = input("Enter a number to be squared: ")
squared = square(num)
print "The square is: ", squared
print "The original num was:", n
def square(n):
return n * n
main()
scope.py 8/9
[top][prev][next]
# scope.py
# Program illustrating scope
# Note: NOT good coding style
# by Sara Sprenkle
def main():
n = 30
e = 0
f = 2
g = 3
h = 4
print "\nBefore the call to function1,"
print "n = ", n
print "e = ", e
# QUESTION: How to change function1's call to execute other branch?
i = function1(e, f, g, h)
print "\nAfter the call to function1,"
print "n = ", n
print "e = ", e
print "i = ", i
def function1(a, b, c, d):
# QUESTION: What would happen if the following line was commented
# out?
n = 400
print "\nIn function1, "
print "n = ", n
print "a = ", a
if a >= 1 :
a += b+n;
print "a = ", a, "after being modified"
return a
else :
c += d+n
return c
main()
swap.py 9/9
[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)
# at the end, y should be 5 and x should be 7
print "x =", x
print "y =", y
Generated by GNU enscript 1.6.4.