Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Mad-Libs/Python/mad-libs.py
Normal file
20
Task/Mad-Libs/Python/mad-libs.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import re
|
||||
|
||||
# Optional Python 2.x compatibility
|
||||
#try: input = raw_input
|
||||
#except: pass
|
||||
|
||||
template = '''<name> went for a walk in the park. <he or she>
|
||||
found a <noun>. <name> decided to take it home.'''
|
||||
|
||||
def madlibs(template):
|
||||
print('The story template is:\n' + template)
|
||||
fields = sorted(set( re.findall('<[^>]+>', template) ))
|
||||
values = input('\nInput a comma-separated list of words to replace the following items'
|
||||
'\n %s: ' % ','.join(fields)).split(',')
|
||||
story = template
|
||||
for f,v in zip(fields, values):
|
||||
story = story.replace(f, v)
|
||||
print('\nThe story becomes:\n\n' + story)
|
||||
|
||||
madlibs(template)
|
||||
Loading…
Add table
Add a link
Reference in a new issue