This is the code for my Mad Libs game. I was confused yesterday, as running this in the “Programming for Everybody” web interpreter would cause the game to loop after reaching the end, going back up to the try/except block, then end after the second run through the story. It didn’t make any sense to me, and I tried all sorts of ways to end the loop, but in the end, it seems it’s a quirk of the Python Playground, not an issue with the loop. It works fine when I run it in terminal and in the Online Python Tutor.
inp = None def quick_kill(): if inp == "Q" or inp == "q": print "Goodbye!" raise SystemExit print "Hello! Are you ready to play...MaD LiBs?!" print "Gahahahaha!" print "(type a single Q or q and press enter to quit instantly)" quick_kill() inp = raw_input("\nOk, first up, gimme an adverb! ") quick_kill() adverb1 = inp inp = raw_input("Now gimme an adjective! ") quick_kill() adjective1 = inp inp = raw_input("Now I need a noun! Teeheehee! ") quick_kill() noun1 = inp inp = raw_input("Another noun! ") quick_kill() noun2 = inp inp = raw_input("Another noun! But make it plural! ") quick_kill() plural_noun1 = inp inp = raw_input("Now I need an adjective! Honk! ") quick_kill() adjective2 = inp inp = raw_input("I need another adjective here! ") quick_kill() adjective3 = inp inp = raw_input("I need a verb here, past tense! ") quick_kill() past_verb1 = inp inp = raw_input("I need a noun right here, buddy! ") quick_kill() noun3 = inp inp = raw_input("Gimme a location! ") quick_kill() location = inp while True: inp = raw_input("I need a number here! Digits only, please! ") quick_kill() try: inp = int(inp) break except: print "\nYou need to enter numerals, not letters (i.e. 6, not six). Let's start over.\n" continue number = inp inp = raw_input("I need a plural noun, STAT! ") quick_kill() plural_noun2 = inp inp = raw_input("Ooh, this is gonna be so good! I need another past tense verb! ") quick_kill() past_verb3 = inp inp = raw_input("I need another past tense verb here! Thank you very much! ") quick_kill() past_verb4 = inp print "\nWoo! All down! Oh boy, oh boy, this is gonna be sooo funny! I can't wait!\n" print "..." print "..." print "..." print "\nHere's our story!\n" print "Mad Libs is a %s %s word game where one %s prompts players for a %s of %s to substitute for blanks in a story, before reading the - often %s or %s - story aloud. The game is frequently %s as a party game or as a %s. The game was invented in %s, and more than %d copies of Mad Libs %s have been %s since the series was first %s in 1958." % (adverb1, adjective1, noun1, noun2, plural_noun1, adjective2, adjective3, past_verb1, noun3, location, number, plural_noun2, past_verb3, past_verb4) print "\nGahahahahahahahahahaha" print "hahahahahahahahahahaha" print "hahahahahahahahahahaha!" print "\nThank you, and good night!" raise SystemExit
Apparently, I can do the raw_input section a lot simpler and cleaner if I make a nested function called a closure, but as-is this is functional. It's just ugly. I haven't messed with nested functions yet.
I made this to test out tuples and using a while loop for error correction, and wound up learning about something else entirely.
Actually, I learned two new things, as while trying to figure out what was wrong with my loop, I read up on different exit commands, and discovered that the way I've been exiting (with "quit()") should only be used in the interpreter, and isn't considered proper for production code. More on exit commands: http://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used









