Sue Evans & James MacGlashan
Adapted from the CS1 Course at Swarthmore College by Lisa Meeden
Hit the space bar for next slide
Let's write a program that "sings" Old MacDonald's Farm
# Filename: farm1.py # Written by: Sue Evans # Date: 7/31/09 # Section: All # Email: bogar@cs.umbc.edu # # This program "sings" Old MacDonald's Farm def main(): print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a cow, Ee-igh, Ee-igh, Oh!" print "With a moo, moo here and a moo, moo there." print "Here a moo, there a moo, everywhere a moo, moo." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" main()
Let's see if it works
linuxserver1.cs.umbc.edu[117] python farm1.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a cow, Ee-igh, Ee-igh, Oh! With a moo, moo here and a moo, moo there. Here a moo, there a moo, everywhere a moo, moo. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[118]
Of course it works. There was nothing difficult about writing this code, except I got tired of typing "Ee-igh, Ee-igh, Oh!" So whenever you find yourself typing the same code over and over again, you should make a module out of that code by putting it in a function.
def <function-name>(<parameters>): <block>
def chorus(): print "Ee-igh, Ee-igh, Oh!"
# Filename: farm2.py # Written by: Sue Evans # Date: 7/31/09 # Section: All # Email: bogar@cs.umbc.edu # # This program "sings" Old MacDonald's Farm # making use of the chorus() function. # chorus() prints the chorus of Old MacDonald's farm # Inputs: none # Outputs: none def chorus(): print "Ee-igh, Ee-igh, Oh!" def main(): print "Old MacDonald had a farm,", chorus() print "And on that farm he had a cow,", chorus() print "With a moo, moo here and a moo, moo there." print "Here a moo, there a moo, everywhere a moo, moo." print "Old MacDonald had a farm,", chorus() main()
linuxserver1.cs.umbc.edu[120] python farm2.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a cow, Ee-igh, Ee-igh, Oh! With a moo, moo here and a moo, moo there. Here a moo, there a moo, everywhere a moo, moo. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[121]
We can also define functions that call other functions. Since our song has a repeated line at the beginning and end of the verse, let's write a function called line(). Since the end of the line is "Ee-igh, Ee-igh, Oh!", we'll be calling the function chorus() to do that. Our program now looks like this :
# Filename: farm3.py # Written by: Sue Evans # Date: 7/31/09 # Section: All # Email: bogar@cs.umbc.edu # # This program "sings" one verse of Old MacDonald's Farm # making use of the chorus() and line() functions. # chorus() prints the chorus of Old MacDonald's farm # Inputs: none # Outputs: none def chorus(): print "Ee-igh, Ee-igh, Oh!" # line() prints the repeated line of Old MacDonald's farm # Inputs: none # Outputs: none def line(): print "Old MacDonald had a farm,", chorus() def main(): line() print "And on that farm he had a cow,", chorus() print "With a moo, moo here and a moo, moo there." print "Here a moo, there a moo, everywhere a moo, moo." line() main()
Be careful not to define functions with the same name, or you will overwrite the previous function definition without warning!
>>> def duplicateName(): ... print "First Definition" ... >>> duplicateName() First Definition >>> def duplicateName(): ... print "Second Definition" ... >>> duplicateName() Second Definition
# verse() prints an entire verse of Old MacDonald's farm # Inputs: animal - an animal's name # sound - the sound that animal makes # Outputs: none def verse(animal, sound): print line() print "And on that farm he had a %s," % (animal), chorus() print "With a %s, %s here" % (sound, sound), print "and a %s, %s there" % (sound, sound) print "Here a %s, there a %s," % (sound, sound), print "everywhere a %s, %s." % (sound, sound) line()
print "And on that farm he had a %s," % (animal),
def main() : verse("cow", "moo") main()
linuxserver1.cs.umbc.edu[149] python farm4.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a cow, Ee-igh, Ee-igh, Oh! With a moo, moo here and a moo, moo there Here a moo, there a moo, everywhere a moo, moo. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[150]
def main() : verse("oink", "pig") main()
linuxserver1.cs.umbc.edu[107] python farm7.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a oink, Ee-igh, Ee-igh, Oh! With a pig, pig here and a pig, pig there Here a pig, there a pig, everywhere a pig, pig. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[108]
def main(): anAnimal = "pig" aSound = "oink" verse(anAnimal, aSound) main()
produces the following output :
linuxserver1.cs.umbc.edu[153] python farm6.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a pig, Ee-igh, Ee-igh, Oh! With a oink, oink here and a oink, oink there Here a oink, there a oink, everywhere a oink, oink. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[154]
Since we've written these functions, we can easily sing as many verses as we want with very little additional code. In fact, that additional code will be in main(). Here's the final edit of the program.
# Filename: farm5.py # Written by: Sue Evans # Date: 7/31/09 # Section: All # Email: bogar@cs.umbc.edu # # This program "sings" many verses of Old MacDonald's Farm # making use of the chorus(), line() and verse() functions. # chorus() prints the chorus of Old MacDonald's farm # Inputs: none # Outputs: none def chorus(): print "Ee-igh, Ee-igh, Oh!" # line() prints the repeated line of Old MacDonald's farm # Inputs: none # Outputs: none def line(): print "Old MacDonald had a farm,", chorus() # verse() prints an entire verse of Old MacDonald's farm # Inputs: animal - an animal's name # sound - the sound that animal makes # Outputs: none def verse(animal, sound): print line() print "And on that farm he had a", animal, "", chorus() print "With a %s, %s here" % (sound, sound), print "and a %s, %s there" % (sound, sound) print "Here a %s, there a %s," % (sound, sound), print "everywhere a %s, %s." % (sound, sound) line() def main(): verse("cow", "moo") verse("pig", "oink") verse("hen", "cluck") verse("duck", "quack") verse("cat", "meow") main()
and the output looks like this :
linuxserver1.cs.umbc.edu[109] python farm5.py Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a cow Ee-igh, Ee-igh, Oh! With a moo, moo here and a moo, moo there Here a moo, there a moo, everywhere a moo, moo. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a pig Ee-igh, Ee-igh, Oh! With a oink, oink here and a oink, oink there Here a oink, there a oink, everywhere a oink, oink. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a hen Ee-igh, Ee-igh, Oh! With a cluck, cluck here and a cluck, cluck there Here a cluck, there a cluck, everywhere a cluck, cluck. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a duck Ee-igh, Ee-igh, Oh! With a quack, quack here and a quack, quack there Here a quack, there a quack, everywhere a quack, quack. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! And on that farm he had a cat Ee-igh, Ee-igh, Oh! With a meow, meow here and a meow, meow there Here a meow, there a meow, everywhere a meow, meow. Old MacDonald had a farm, Ee-igh, Ee-igh, Oh! linuxserver1.cs.umbc.edu[110]
The code we produced is certainly preferable to the naive way to accomplish the same task :
# Filename: farm1.py # Written by: Sue Evans # Date: 7/31/09 # Section: All # Email: bogar@cs.umbc.edu # # This program "sings" Old MacDonald's Farm def main(): print print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a cow, Ee-igh, Ee-igh, Oh!" print "With a moo, moo here and a moo, moo there." print "Here a moo, there a moo, everywhere a moo, moo." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a pig, Ee-igh, Ee-igh, Oh!" print "With a oink, oink here and a oink, oink there." print "Here a oink, there a oink, everywhere a oink, oink." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a hen, Ee-igh, Ee-igh, Oh!" print "With a cluck, cluck here and a cluck, cluck there." print "Here a cluck, there a cluck, everywhere a cluck, cluck." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a duck, Ee-igh, Ee-igh, Oh!" print "With a quack, quack here and a quack, quack there." print "Here a quack, there a quack, everywhere a quack, quack." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" print "And on that farm he had a cat, Ee-igh, Ee-igh, Oh!" print "With a meow, meow here and a meow, meow there." print "Here a meow, there a meow, everywhere a meow, meow." print "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!" main()
A great thing about using the version with functions is that we can add new animals to the song quickly and easily. Adding one more call to the function verse() produces another whole verse with the animal and sound passed.
verse("sheep", "baa")
# isPrime() takes a positive integer as an argument # and returns True if that integer is a prime number # and False if the number is not prime. # Input: a positive integer, num # Output: either True or False # Assumptions: num will be an integer > 1 def isPrime(num): # try to evenly divide num by all # numbers between 2 and num - 1 for i in range(2, num): # if num is evenly divisble by this # number, then it isn't prime if num % i == 0: return False # if num wasn't divisable by any of # those numbers, then it's prime return True def main(): # print primes from 2 to 11, inclusive for i in range (2, 12): if isPrime(i): print i, main()
Here's the output.
linuxserver1.cs.umbc.edu[118] python isPrime.py 2 3 5 7 11 linuxserver1.cs.umbc.edu[119]
# chorus() prints the chorus of Old MacDonald's farm # Inputs: none # Outputs: none def chorus(): print "Ee-igh, Ee-igh, Oh!"
# chorus() prints the chorus of Old MacDonald's farm # Inputs: none # Outputs: none # Effect: prints "Ee-igh, Ee-igh, Oh!" def chorus(): print "Ee-igh, Ee-igh, Oh!"
Write a program that will calculate the volume and surface area of a sphere from its radius, given as user input. Your program must have the following functions:
Here are some useful formulas:
SA = 4 π r2
Vol = 4/3 π r3
Hints: