Learning Diary - Python Mad Libs
Life before video games was terrible in many ways
I am still working through Code Academy’s course. Stopping to spin my own version of the Pro Course lessons. Not that I’m cheap or anything (okay I’m a little cheap), but this was primarily to challenge myself to build things without following along and having someone check my code. I’ll be working for these the next couple of weeks, as I look for more advanced projects to keep me busy.
This is a bit of a simple project, I don’t have two different versions this time. A simple mad lib, this was rather straightforward. If you were born in the 90’s, these were books that had you list out different parts of speech and then fill in a premed sentence to childish amounts of hilarity. Okay, so there wasn’t much hilarity. A cursory search of Amazon shows that these are still a thing, so maybe you 90’s kids weren’t without a frame of reference.
We’re still basically working with the command line for user input, or more accurately the debugger section of VS Code. The script asks you for ten words, referring for them by their part of speech. This may be the first time I’ve used the word gerund in a script, and probably the first time I’ve used it period since college.
In celebration of WWDC, I wrote up some marketing speak from Pear Computer. I drop in the words from the user by using %s. After the long string is written out I use the variables from the user input in order afterward. This is done by adding another % and then putting the variables in order. I'm not sure if you need the parentheses, but it does look neater that way.
I’m not really sure what else I could do here. A few things come to mind but are a bit out of my skills now. One would be actually giving you more than one Mad Lib. Having ten or so that randomly change each time that the script runs would be more useful. I’d also like a real UI, so you’re not simply entering text into the command line. That would also help with the formatting of the output, which can get jumbled up depending on the size of your screen. Like the tip calculator, I may want to come back to this one as I keep learning more.
#Gather Inputs
adj1 = raw_input("Enter an adjective ")
non1 = raw_input("Enter a noun ")
non2 = raw_input("Enter a plural noun ")
ger1 = raw_input("Enter a gerund ")
non3 = raw_input("Enter a noun ")
col1 = raw_input("Enter a color ")
ver1 = raw_input("Enter a verb ")
ger2 = raw_input("Enter a gerund ")
non4 = raw_input("Enter a noun ")
adj2 = raw_input("Enter an adjective ")
#Output Result
print "We here at Pear Computer are happy to bring you our newest device, the %s phone. Made with the %s in mind, our %s devoted hundreds of hours %s the %s by hand. It's only available in our favorite color %s. All of you gamers will be sure to %s out your dreams by %s right from your %s. We're excited that you find all this %s great." % (adj1, non1, non2, ger1, non3, col1, ver1, ger2, non4, adj2)
So there you have it. Still not much to look at, but it’s progress. I like these projects because they aren’t entirely focused on IT automation. Getting more of a breadth of experience with the language before directly applying it.
Rants and Reviews. Mostly just BS and Affiliate Links.
Follow on Mastodon