forked from 4dsolutions/Python5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmadlibs_v2.py
More file actions
65 lines (48 loc) · 2.45 KB
/
madlibs_v2.py
File metadata and controls
65 lines (48 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/env python3.4
"""
Madlibs provide exercise in 'string substitution' wherein
we have a Template text (something with blanks in it, like
a Form to fill out), and then the information to complete
it. There is not just one way to do it.
Real use case: generate scene description language
for povray (povray.org) by filling in various templates
with computed values. Good for rendering perspective
stills called "ray tracings" (see povray.org gallery
for examples).
Demonstrate one of those ways: use str.format( )
"""
# Example using .format()
# (pretending to be a limerick or haiku or something)
silly_story = """There once was a boy from {any_town} who
loved to eat lots of {junk_food}. He ate so many
{junk_food} that one day (the people of {any_town}
claim) he actually turned into a {something}.""" # note repeating keys
# now lets get user input:
the_data = {} # empty dict (curly braces -- not a set)
print("I need to ask you for three things...")
for key in ["junk_food", "any_town", "something"]: # strings we need
usr = input("Please give me a value for [" + key + "]: ")
# usr input could have issues so 'guard code' might go here
# lets just hope it all makes sense for now...
the_data[key] = usr # store answer as value to the key
print("Thank you for your time") # be polite
# pass in keyward / named arguments
filled_in = silly_story.format( any_town = the_data["any_town"],
junk_food = the_data["junk_food"],
something = the_data['something'])
print("OK, here is your silly story of the day: \n") # <-- add a newline
print("{:^20}".format("silly story".title())) # center within 20 spaces
print(filled_in)
# However there's an easier way to get all those keyed arguments
# from the_data: explode it with **the_data
# pass in keyward / named arguments
# dict keys need to match the place holder names in this pattern
# you'll get a KeyError if any place holder name goes unmatched.
filled_in = silly_story.format(**the_data) # turns dict into named args
# in general, dicts may be exploded into keyword arguments (useful!)
print("\n\n... and here it is a second time: \n") # <-- adding newlines
print("{:^20}".format("silly story".title())) # center within 20 spaces
print(filled_in)
# Now please try something similar, where you solicit input from
# the user in a loop and use that input to fill in some template
# or form. It might be a passport application or formal document.