-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchap9_mad_libs.py
More file actions
36 lines (29 loc) · 1.16 KB
/
chap9_mad_libs.py
File metadata and controls
36 lines (29 loc) · 1.16 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
from random import choice as choose
from random import seed
seed()
# lists of words to use (will be randomized)
animal = ['panda', 'bear', 'python', 'dog', 'roach']
action = ['walked', 'jumped', 'ran', 'flew', 'swam']
# create text string with sample words
# "The ADJECTIVE [animal] [past tense action] to the NOUN and then VERB.
# A nearby NOUN was unaffected by these events."
string = f'The ADJECTIVE {choose(animal)} {choose(action)} to the NOUN and ' \
'then VERB. A nearby NOUN was unaffected by these events.\n'
# save string into a file
fd = open('chap9_madLibs.txt', mode='w')
fd.write(string)
fd.close()
# read newly created file --> new string
fd = open('chap9_madLibs.txt', mode='r')
string = fd.read()
fd.close()
# get user inputs (adjective, noun, verb, noun), uptade string.
for item in ['ADJECTIVE', 'NOUN', 'past tense VERB', 'NOUN']: # using string.replace()
prompt = 'enter a ' + item + ': '
user_input = input(prompt)
string = string.replace(item, user_input, 1)
# create new file with new string, output string to console
fd = open('chap_madLibs2.txt', mode='w')
fd.write(string)
fd.close()
print(string)