# run this and include an export file, ie: python ex22b.py ex22b.txt # after completing the madlib it will output to the specified file. from sys import argv # allows us to specify the file we are going to use for saving the madlib. # from os.path import exists # this was in here to notify if the file exists or not. didnt end up using that. import os # using this to use the screen clear function. script, filename = argv # sets filename to the file we specified in running the script. os.system('cls' if os.name == 'nt' else 'clear') # clears screen. # wanted to give the option to clear the file, or stop the script. print "We are going to clear %r. If you don't want this, press CTRL+C now. Otherwise hit RETURN." % filename raw_input() # waits for the RETURN to continue. target = open(filename, 'w') # opens the files target.truncate() # clears it out print "File Erased, and ready to write a new one! hit RETURN to get started!" raw_input() # Wait for the RETURN to continue, and the next line clears screen. os.system('cls' if os.name == 'nt' else 'clear') # simple multi line print. print """ Lets have a little MadLib Fun! I am going to ask you for a few words. Be creative and lets make a story! """ # set the variables used in the madLib. print "Give me an:" adj1 = raw_input("Adjective:") adj2 = raw_input("Adjective:") num = raw_input("Number:") food = raw_input("Type of food (plural):") adj3 = raw_input("Adjective:") noun = raw_input("Noun:") adj4 = raw_input("Adjective:") print "Got the data! Press RETURN to save your MadLib to %r, and display it!" % filename raw_input() # above lets the user know they are done, below clears the screen again to display only the finished MadLib os.system('cls' if os.name == 'nt' else 'clear') # this sets the line1 with the story and fills in the variables. line1 = """ Fractured Aesop's Fables\n The Mole and his Mother By: Aesop (Fractured by Us)\n A %s Mole, a creature blind from birth, once said to his %s Mother: "I am sure that I can see, Mother!" In an effort to prove to him his mistake, his Mother placed before him %s %s, and asked, "What is it?" The %s Mole said, "It is a %s." His Mother exclaimed: "My %s son, I am afraid that you are not only blind, but that you have lost your sense of smell." """ % (adj1, adj2, num, food, adj3, noun, adj4) target.write(line1) # this writes the story to the specified file. # the rest opens, and prints back the file to screen. target = open(filename, 'r') def print_all(f): print f.read() print "Here is your story!:\n" print_all(target) # now lets clear it out, and exit it out. print "Hit enter to get back to your command prompt!" raw_input() os.system('cls' if os.name == 'nt' else 'clear') target.close()