SOlve 4.16 lab
This commit is contained in:
parent
0674442945
commit
549923cc01
|
@ -0,0 +1,42 @@
|
||||||
|
|
||||||
|
# Construct a mad lib
|
||||||
|
class mad_lib:
|
||||||
|
|
||||||
|
# Initalize a constructor for python mad lib
|
||||||
|
def __init__(self, lib):
|
||||||
|
# lib is a value passed in during the construction of this class
|
||||||
|
self.text = lib
|
||||||
|
self.input = input().split()
|
||||||
|
|
||||||
|
# Replace %text% with user input
|
||||||
|
if '%first_name%' in self.text:
|
||||||
|
self.text = self.text.replace('%first_name%', self.first_name())
|
||||||
|
if '%location%' in self.text:
|
||||||
|
self.text = self.text.replace('%location%', self.location())
|
||||||
|
if '%whole_number%' in self.text:
|
||||||
|
self.text = self.text.replace('%whole_number%', self.whole_number())
|
||||||
|
if '%plural_noun%' in self.text:
|
||||||
|
self.text = self.text.replace('%plural_noun%', self.plural_noun())
|
||||||
|
|
||||||
|
|
||||||
|
def first_name(self):
|
||||||
|
#return input("A first name: ")
|
||||||
|
return self.input.pop()
|
||||||
|
|
||||||
|
def location(self):
|
||||||
|
#return input("A location: ")
|
||||||
|
return self.input.pop()
|
||||||
|
|
||||||
|
def whole_number(self):
|
||||||
|
#return input("A whole number: ")
|
||||||
|
return self.input.pop()
|
||||||
|
|
||||||
|
def plural_noun(self):
|
||||||
|
#return input("A plural noun: ")
|
||||||
|
return self.input.pop()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Construct a mad lib
|
||||||
|
md = mad_lib('%first_name% went to %location% to buy %whole_number% different types of %plural_noun%')
|
||||||
|
|
||||||
|
print(md.text)
|
|
@ -0,0 +1,8 @@
|
||||||
|
from mad_lib import mad_lib
|
||||||
|
|
||||||
|
while True:
|
||||||
|
md = mad_lib('Eating %whole_number% %plural_noun% a day keeps the doctor away.')
|
||||||
|
if 'quit' in md.text:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(md.text)
|
Loading…
Reference in New Issue