patternpythonMinor
Mad Libs Generator
Viewed 0 times
madlibsgenerator
Problem
I created this Mad Libs generator, but I don't know how to get the value of the key "ans" into a format string tuple. I feel like it's not Pythonic and that it's very convoluted and over-complicated.
Check out on REPL.it
```
# Our Mad Lib
madlib = "On the %s trip to %s, my %s friend and I decided to invent a game. Since this would be a rather %s trip, it would need to be a game with %s and %s. Using our %s to %s, we tried to get the %s next to us to play too, but they just %sed at us and %s away. After a few rounds, we thought the game could use some %s, so we turned on the %s and started %s to the %s that came on. This lasted for %s before I got %s and decided to %s. I'll never %s that trip, it was the %s road trip of my %s."
sourceURL = "http://marcelkupka.cz/wp-content/uploads/2013/03/mad.jpg"
# A list storing the blanks for the Mad Lib
blanks = [
{"suggestion": "adjective", "ans": ""},
{"suggestion": "place", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "action verb", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb that ends in ing", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "measurement of time", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "action verb", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "noun, something you can own", "ans": ""}
]
print("Road Trip Mad Lib\nWhen the program asks you, please enter the appropriate word.")
print("There are %i blanks in this Mad Lib. " % (len(blanks)))
# Ask the user for each one
for blank in bl
Check out on REPL.it
```
# Our Mad Lib
madlib = "On the %s trip to %s, my %s friend and I decided to invent a game. Since this would be a rather %s trip, it would need to be a game with %s and %s. Using our %s to %s, we tried to get the %s next to us to play too, but they just %sed at us and %s away. After a few rounds, we thought the game could use some %s, so we turned on the %s and started %s to the %s that came on. This lasted for %s before I got %s and decided to %s. I'll never %s that trip, it was the %s road trip of my %s."
sourceURL = "http://marcelkupka.cz/wp-content/uploads/2013/03/mad.jpg"
# A list storing the blanks for the Mad Lib
blanks = [
{"suggestion": "adjective", "ans": ""},
{"suggestion": "place", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "action verb", "ans": ""},
{"suggestion": "noun, plural", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "verb that ends in ing", "ans": ""},
{"suggestion": "noun", "ans": ""},
{"suggestion": "measurement of time", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "action verb", "ans": ""},
{"suggestion": "verb", "ans": ""},
{"suggestion": "adjective", "ans": ""},
{"suggestion": "noun, something you can own", "ans": ""}
]
print("Road Trip Mad Lib\nWhen the program asks you, please enter the appropriate word.")
print("There are %i blanks in this Mad Lib. " % (len(blanks)))
# Ask the user for each one
for blank in bl
Solution
There are many things here which could be improved. I will however only improve some of them.
-
The golden standard for style in python is PEP 8.
It explains in excruciating detail how to structure your code. I whole heartily recommend skimming through it and follow it.
-
Use the
with the new one. Check here to learn more about Pythons awesome
string formating options.
-
Do not use
-
A bigger concern is that you update
Why not have the
-
You
-
Your commenting is good, however it could be improved by using doc strings.
-
A more severe concern with your code is that it is not modular. You have everything lumped into a single file. Really you should NEVER have that. Create a function named
-
In PEP 8, it is recommended that the maximum linewidth is 79 characters. This is a good rule of thumb. You can use triple quotation marks to format long strings. Here are some other methods to format long strings.
\${}{}\$
-
The golden standard for style in python is PEP 8.
It explains in excruciating detail how to structure your code. I whole heartily recommend skimming through it and follow it.
-
Use the
if __name__ == "__main__": module. It makes your code clearer and reusable. blanksdoes not have to be a list of dicts. It is clearer if it only holds the type
'%s %s' % ('one', 'two')is the old string format while'{} {}'.format('one', 'two')is the new one. I recommend sticking
with the new one. Check here to learn more about Pythons awesome
string formating options.
if len(ans) == 0:is a unorthodox way to change if a string is empty. A more pythonic approach isif not ans.
-
Do not use
quit() a better approach is to make the ans into a while loopfor blank in blanks:
ans = ""
while not ans:
ans = input(blank.capitalize() + "> ")
if not ans:
print("Please don't leave anything blank. It kills the experience.")
blank['ans'] = ans-
A bigger concern is that you update
blank['ans'] = ans, but then immediately move the values into a list. fs = []
# Get the answers from the blanks list
for dictionary in blanks:
fs.append(dictionary['ans'])Why not have the
answers as a list from the start?-
You
capitalize the words on input. However when asking if the user liked the story you do not lower the words. When i write Y it tells me I did not like the story. The key here is .lower().if feedback.lower() == "y":
print("Thanks!")-
Your commenting is good, however it could be improved by using doc strings.
-
A more severe concern with your code is that it is not modular. You have everything lumped into a single file. Really you should NEVER have that. Create a function named
get_user_input which handles all the errors. Another code to ask the user to play again and so forth. A basic structure is something like the followingimport
CONSTANTS
def some_function():
def another_function():
def main()
while True:
some_function()
play_again = input('do you want to play again? [y/n]: ')
if play_again.lower() not in ['y', 'yes', 'ok']:
break
if __name__ == '__main__':
main()-
In PEP 8, it is recommended that the maximum linewidth is 79 characters. This is a good rule of thumb. You can use triple quotation marks to format long strings. Here are some other methods to format long strings.
\${}{}\$
# Our Mad Lib
madlib =
'''
On the {} trip to {}, my {} friend and I decided to invent a game. Since
this would be a rather {} trip, it would need to be a game with {} and {}.
Using our {} to {}, we tried to get the {} next to us to play too, but they
just {}ed at us and {} away. After a few rounds, we thought the game could
use some {}, so we turned on the {} and started {} to the {} that came on.
This lasted for {} before I got {} and decided to {}. I'll never {} that trip,
it was the {} road trip of my {}.
'''
sourceURL = "http://marcelkupka.cz/wp-content/uploads/2013/03/mad.jpg"
# A list storing the blanks for the Mad Lib
blanks = [
"adjective",
"place",
"adjective",
"adjective",
"noun, plural",
"noun, plural",
"noun",
"verb",
"noun",
"verb",
"action verb",
"noun, plural",
"noun",
"verb that ends in ing",
"noun",
"measurement of time",
"adjective",
"action verb",
"verb",
"adjective",
"noun, something you can own"
]
print("Road Trip Mad Lib\nWhen the program asks you, please enter the appropriate word.")
print("There are %i blanks in this Mad Lib. " % (len(blanks)))
# Ask the user for each one
answers = []
for blank in blanks:
ans = ""
while not ans:
ans = input(blank.capitalize() + "> ")
if not ans:
print("Please don't leave anything blank. It kills the experience.")
answers.append(ans)
# The list that stores the format string
# Print the formatted Mad Lib
print(madlib.format(*answers))
feedback = input("Pretty funny, right? [y/n] ")
if feedback.lower() == "y":
print("Thanks!")
else:
print(":( Sorry. I'll try better next time.")
print("\n" + "="*10 + "\nMad Lib sourced from " + sourceURL)Code Snippets
for blank in blanks:
ans = ""
while not ans:
ans = input(blank.capitalize() + "> ")
if not ans:
print("Please don't leave anything blank. It kills the experience.")
blank['ans'] = ansfs = []
# Get the answers from the blanks list
for dictionary in blanks:
fs.append(dictionary['ans'])if feedback.lower() == "y":
print("Thanks!")import
CONSTANTS
def some_function():
def another_function():
def main()
while True:
some_function()
play_again = input('do you want to play again? [y/n]: ')
if play_again.lower() not in ['y', 'yes', 'ok']:
break
if __name__ == '__main__':
main()# Our Mad Lib
madlib =
'''
On the {} trip to {}, my {} friend and I decided to invent a game. Since
this would be a rather {} trip, it would need to be a game with {} and {}.
Using our {} to {}, we tried to get the {} next to us to play too, but they
just {}ed at us and {} away. After a few rounds, we thought the game could
use some {}, so we turned on the {} and started {} to the {} that came on.
This lasted for {} before I got {} and decided to {}. I'll never {} that trip,
it was the {} road trip of my {}.
'''
sourceURL = "http://marcelkupka.cz/wp-content/uploads/2013/03/mad.jpg"
# A list storing the blanks for the Mad Lib
blanks = [
"adjective",
"place",
"adjective",
"adjective",
"noun, plural",
"noun, plural",
"noun",
"verb",
"noun",
"verb",
"action verb",
"noun, plural",
"noun",
"verb that ends in ing",
"noun",
"measurement of time",
"adjective",
"action verb",
"verb",
"adjective",
"noun, something you can own"
]
print("Road Trip Mad Lib\nWhen the program asks you, please enter the appropriate word.")
print("There are %i blanks in this Mad Lib. " % (len(blanks)))
# Ask the user for each one
answers = []
for blank in blanks:
ans = ""
while not ans:
ans = input(blank.capitalize() + "> ")
if not ans:
print("Please don't leave anything blank. It kills the experience.")
answers.append(ans)
# The list that stores the format string
# Print the formatted Mad Lib
print(madlib.format(*answers))
feedback = input("Pretty funny, right? [y/n] ")
if feedback.lower() == "y":
print("Thanks!")
else:
print(":( Sorry. I'll try better next time.")
print("\n" + "="*10 + "\nMad Lib sourced from " + sourceURL)Context
StackExchange Code Review Q#133134, answer score: 7
Revisions (0)
No revisions yet.