# 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();
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
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
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
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
def triple(number):
return (number * 3);
def triple1(text):
return (text + text + text);
print "Tripling 5: " , triple(5)
print "Tripling 'gamer': " , triple1("gamer");
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
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);