In python 3 using this as a starrting point: and this Minimizing the number of i
ID: 3849639 • Letter: I
Question
In python 3
using this as a starrting point:
and this
Minimizing the number of if statements in the verse. The minimum is two. and not using helper functions and produce the following output:
I Know an Old Lady
I know an old lady who swallowed a fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a bird.
How absurd to swallow a bird.
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a cat.
Imagine that to swallow a cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a dog.
My, what a hog, to swallow a dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a goat.
She just opened her throat and in walked the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
I know an old lady who swallowed a cow.
I don't know how she swallowed a cow.
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly.
Perhaps she'll die.
There was an old lady who swallowed a horse.
She's dead, of course.
example like:
Explanation / Answer
Scripts with only three if statements .
old_lady.py
ANIMALS = ('fly.', 'spider,', 'bird.', 'cat.',
'dog.', 'goat.', 'cow.', 'horse.')
LINES = ('I don't know why she swallowed the fly.',
'That wriggled and jiggled and tickled inside her.',
'How absurd to swallow a bird.',
'Imagine that to swallow a cat.',
'My, what a hog, to swallow a dog.',
'She just opened her throat and in walked the goat.',
'I don't know how she swallowed a cow.',
'She's dead, of course.')
def title():
print('I Know an Old Lady')
def verse(n):
i = 1
while(n >=0):
# First if condition
# This line is required only for the first time when our function is called
if i == 1:
print('I know an old lady who swallowed a {}'.format(ANIMALS[n]))
i += 1
print(LINES[n])
# Second if condition
# To stop the loop when she swallows the horse
if n == 7:
return None
# Third if condition
# This line is required every time but not the first time
if n!=0:
print("She swallowed the {0} to catch the {1}.".format(ANIMALS[n],ANIMALS[n-1]))
n -= 1
print("Perhaps she'll die.")
main.py
import old_lady
# the driver function
def main():
# print out the title
old_lady.title()
# print out the verses
for num in range(len(old_lady.ANIMALS)):
old_lady.verse(num)
# run the program
main()
PLEASE RATE !!!
THANK YOU!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.