chapter 5 : Functions: Mad Lib

example 5.1 page no : 152

In [1]:
# function prototype (declaration)
def instructions():
    print "Welcome to the most fun you've ever had with text!\n";
    print "Here's how to play the game. . .\n";

instructions();
Welcome to the most fun you've ever had with text!

Here's how to play the game. . .

example 5.2 page no : 156

In [2]:
def askYesNo1():
    while True:
        print "Please enter 'y' or 'n' : ",
        response1 = 'y' #raw_input()
        if (response1 == 'y' or response1 == 'n'):
            return response1

def askYesNo2(question):
    while True:
        print question ,
        response1 = 'n' #raw_input()
        if (response1 == 'y' or response1 == 'n'):
            return response1


answer1 = askYesNo1();
print "Thanks for answering: " , answer1  
answer2 = askYesNo2("Do you wish to save your game?");
print "Thanks for answering: " , answer2 
Please enter 'y' or 'n' :  Thanks for answering:  y
Do you wish to save your game? Thanks for answering:  n

example 5.3 page no : 162

In [3]:
def func():
    var = -5; # local variable in func()
    print "In func() var is: " , var 

var = 5 # local variable in main()
print "In main() var is: " , var
func();
print "Back in main() var is: " , var
print "In main() in a new scope var is: " , var
print "Creating new var in new scope.";
var = 10; # variable in new scope, hides other variable named var
print "In main() in a new scope var is: " , var 

print "At end of main() var created in new scope no longer exists.";
print "At end of main() var is: " , var 
In main() var is:  5
In func() var is:  -5
Back in main() var is:  5
In main() in a new scope var is:  5
Creating new var in new scope.
In main() in a new scope var is:  10
At end of main() var created in new scope no longer exists.
At end of main() var is:  10

example 5.4 page no : 167

In [4]:
glob = 10; # global variable

def access_global():
    global glob
    print "In access_global() glob is: " , glob

def hide_global():
    glob = 0 # hide global variable glob
    print "In hide_global() glob is: " , glob

def change_global():
    glob = -10; # change global variable glob
    print "In change_global() glob is: " , glob


print "In main() glob is: " , glob
access_global();
hide_global();
print  "In main() glob is: " , glob
change_global();
print "In main() glob is: " , glob
In main() glob is:  10
In access_global() glob is:  10
In hide_global() glob is:  0
In main() glob is:  10
In change_global() glob is:  -10
In main() glob is:  10

example 5.5 page no : 172

In [6]:
def askNumber(high,low=1):
    a = high + 2
    while True:
        print "Please enter a number" , " (" , low , " - " , high , "): "
        num = a #int(raw_input())
        if (num > high or num < low):
            return num


number = askNumber(5);
print "Thanks for entering: " , number
number = askNumber(10, 5);
print "Thanks for entering: " , number
Please enter a number  ( 1  -  5 ): 
Thanks for entering:  7
Please enter a number  ( 5  -  10 ): 
Thanks for entering:  12

example 5.6 page no : 175

In [7]:
def triple(number):
    return (number * 3);

def triple1(text):
    return (text + text + text);

print "Tripling 5: " , triple(5) 
print "Tripling 'gamer': " , triple1("gamer");
Tripling 5:  15
Tripling 'gamer':  gamergamergamer

example 5.7 page no : 178

In [8]:
def radiation( health):
    return (health / 2);

health = 80;
print "Your health is " , health 
health = radiation(health);
print "After radiation exposure your health is " , health 
health = radiation(health);
print "After radiation exposure your health is " , health 
health = radiation(health);
print "After radiation exposure your health is " , health 
Your health is  80
After radiation exposure your health is  40
After radiation exposure your health is  20
After radiation exposure your health is  10

example 5.8 page no : 181

In [9]:
def askText(prompt):
    print prompt ,
    a = raw_input()
    return a

def askNumber(prompt):
    print prompt ,
    return int(raw_input())

def tellStory(name, noun, number, bodyPart, verb):
    print "\nHere's your story:";
    print "The famous explorer " ,;
    print name ,
    print " had nearly given up a life-long quest to find",
    print "The Lost City of ",
    print noun,
    print " when one day, the ",
    print noun,
    print " found the explorer."
    print "Surrounded by ",
    print number,
    print " " , noun,
    print ", a tear came to ",
    print name , "'s ",
    print bodyPart , "."
    print "After all this time, the quest was finally over. ",
    print "And then, the ",
    print noun 
    print "promptly devoured ",
    print name , ". "
    print "The moral of the story? Be careful what you ",
    print verb,
    print " for.",


print "Welcome to Mad Lib.\n";
print "Answer the following questions to help create a new story.";
name = 'jay' #askText("Please enter a name: ");
noun = 'go' #askText("Please enter a plural noun: ");
number = 10 #askNumber("Please enter a number: ");
bodyPart = 'nothing' #askText("Please enter a body part: ");
verb = 'verb' #askText("Please enter a verb: ");
tellStory(name, noun, number, bodyPart, verb);
Welcome to Mad Lib.

Answer the following questions to help create a new story.

Here's your story:
The famous explorer  jay  had nearly given up a life-long quest to find The Lost City of  go  when one day, the  go  found the explorer.
Surrounded by  10   go , a tear came to  jay 's  nothing .
After all this time, the quest was finally over.  And then, the  go
promptly devoured  jay . 
The moral of the story? Be careful what you  verb  for.