Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There was an Old Lady There was an old lady who swallowed a fly. I don't know why she swallowed the fly. Perhaps she'll die.

There was an Old Lady

There was an old lady who swallowed a fly. I don't know why she swallowed the fly. Perhaps she'll die.

There was 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.

There was 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.

There was 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.

There was 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.

{insert your own verse here follow the pattern of the other verses. (not required for the Minimal Version. one point for creativity!!}

There was an old lady who swallowed a horse. She's dead, of course.

Instructions

Your task for this assignment is to write a Python script that will print out the song, possibly including your own verse at the indicated location. Of course, you could do this with a gajillion calls to print, arranged one right after another, but you won't, will you? (because if you do, it will earn you lousy points for the assignment. The point of this assignment is to get a good sense of refactoring, that is, designing with functions to organize code and to minimize redundancy.

Starting Point

For the starting point, you need to write the functions so that the song is printed out as it appears above.

In your design, the main function should have 6 function calls for the 6 verses shown above. It will look more or less like the following:

def main(): # fly verse verse1() # spider verse verse2() # bird verse verse3() # cat verse verse4() # dog verse verse5() # horse verse lastverse() # call main main()

Some notes for the starting point:

  • The verse functions shall take no parameters. They shall appear exactly as shown here.
  • There shall be a blank line that is output between each of the verses. (The blank line that shall be printed as part of the individual verse functions, at the end of each of the verses.)
  • The script function shall include a title function. Like the verse function, the title function shall be parameterless. It is up to you to add a call to main for the title function.
  • The main function shall contain only calls to the title and verse functions.

The Standard Level adds two enhancements to the Minimal Level:

  • A new verse between the dog verse and the horse verse. This verse is output by a function named verse6. Call verse6 between the call to verse5 and lastverse.
  • Refactoring the verse functions to reduce the redundancy.

Note: for the Standard Level, the bulk of the main function will look like the following. Simply a series of function calls with no arguments in any of them.

# fly verse verse1() # spider verse verse2() # bird verse verse3() # cat verse verse4() # dog verse verse5() # new verse verse6() # horse verse lastverse()

In the minimal level, we started the process of refactoring, by moving the common last two lines into a function that could be called in most of the verse functions. For the standard version, you need to find other opportunities for refactoring (note use of plural) and move the repetitive code into reusable functions.

Look into each section: what is repeated within a section? What is repeated from one section to the next? The portions that you identify as repeated, are they repeated exactly? Or do they differ? Do they differ a little or a lot? Hint: The refactoring for this assignment is probably the most straight forward at the line level.

The answers to these questions will guide you in your function design. Just like in the refactoring demo, the refactoring of the song verses will entail creating new functions to handle the common code. Realize also, that if lines of code are almost exact, but differ by one or two words, you can use parameters to account for the minor differences.

To simplify the code for the Standard level, we will make a simple change to the song. Notice that the spider lines end with a comma. All the other lines end with a period. This is due to the structure of English. "That wriggled and jiggled and tickled inside her." is not a full sentence. It's a dependent clause that cannot stand alone. Hence, the comma after "spider" on the previous line. For the Standard version simplification, you can substitute a period for the "spider" comma.

There was 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.

There was 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.

We don't know about lists yet, so there are kinds of clean-up (refactoring) which are not yet possible for us.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions