Question

This assignment focuses on writing functions. These functions will be used to make the structure of...

This assignment focuses on writing functions. These functions will be used to make the structure of the program cleaner and clearer. They will use parameters. There is no need to use return values in the core of this assignment. (The challenge option might be easier if you use return values. For more information, see section 5.7 in the text.)

Overview

The children's song that we'll work on is "Old McDonald". In case you aren't familiar with it, here are a couple example verses:

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a chicken, E-I-E-I-O.
With a cluck-cluck here, and a cluck-cluck there.
Here a cluck, there a cluck, everywhere a cluck-cluck.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a cow, E-I-E-I-O.
With a moo-moo here, and a moo-moo there.
Here a moo, there a moo, everywhere a moo-moo.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a duck, E-I-E-I-O.
With a quack-quack here, and a quack-quack there.
Here a quack, there a quack, everywhere a quack-quack.
Old McDonald had a farm, E-I-E-I-O.

The very observant among you may notice that this version of the song has been simplified slightly.
[Participation fodder: what's the simplification? ]

Standard Version

For the standard version:

  • Complete all the work for the minimal version.
  • Additionally, write a fourth function function that shall ask the user for an animal and its sound.
  • Name this function newVerse. The newVerse function is parameterless, that is, it has empty parens.
  • After collecting this information from the user, the newVerse function shall print out another verse with the user-provided animal and sound values.
  • There is no input validation needed for the user's input for animal or sound.
  • The new function shall not call the print function to print out the verse. Instead, the newVerse function shall call the verse function to generate its output.
  • In main, following the other function calls, call the newVerse function. Note, this prints out a fourth verse to the song.
  • In main, following the call to the new verse function, the script shall ask the user if they want a fifth verse. Depending on the user's answer, the new verse function shall be called a second time.
  • This question about the fifth verse shall expect the answers: yes / no.
  • It is reasonable to tell the users what the expected values are.
  • The code should be robust enough to take some default action if the user doesn't give one of the "expected" answers.
  • The default action shall be to prompt for the animal and sound for the fifth verse.

The last two three bullet points can be translated as:

There are only two possible answers for the "Do you want a fifth verse?" question. Tell the user what the choices are. If the user picks one of them, do that. If the user's input doesn't match either of your two choices, assume that the user wants to do a fifth verse.

Sample Run

Here is an example of what the Standard assignment will look like, when run. The user input is shown in bold blue characters.

Old McDonald

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a chicken, E-I-E-I-O.
With a cluck-cluck here, and a cluck-cluck there.
Here a cluck, there a cluck, everywhere a cluck-cluck.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a cow, E-I-E-I-O.
With a moo-moo here, and a moo-moo there.
Here a moo, there a moo, everywhere a moo-moo.
Old McDonald had a farm, E-I-E-I-O.

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a duck, E-I-E-I-O.
With a quack-quack here, and a quack-quack there.
Here a quack, there a quack, everywhere a quack-quack.
Old McDonald had a farm, E-I-E-I-O.

Enter an animal: horse
Enter the sound the horse makes: neigh

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a horse, E-I-E-I-O.
With a neigh-neigh here, and a neigh-neigh there.
Here a neigh, there a neigh, everywhere a neigh-neigh.
Old McDonald had a farm, E-I-E-I-O.

Do you want to have a fifth verse (yes/no)? nah

Enter an animal: pig
Enter the sound the pig makes: oink

Old McDonald had a farm, E-I-E-I-O.
And on that farm he had a pig, E-I-E-I-O.
With a oink-oink here, and a oink-oink there.
Here a oink, there a oink, everywhere a oink-oink.
Old McDonald had a farm, E-I-E-I-O.

In this example, for the obligatory fourth verse, the user entered horse and neigh. Then the user was asked about the fifth verse. The "acceptable answers" are "yes" and "no", as indicated in the question. The user's response was "nah". Since it doesn't match either of the expected answers ("yes" or "no"), the default action (the assumption) is to call the newVerse function and get the animal and sound for a fifth verse. The user entered pig and oink for the fifth verse.

Challenge Version

For the extra-credit version:

  • Complete all the work for the standard version.
  • Update the verse function to handle animal names or sounds that begin with a vowel. For the purposes of this assignment, there are exactly five vowels: A E I O U. Case does not matter. Also, we will not worry about the pronunciation, just the spelling. So, it would be "an unicorn" or "a honor" (Yes, I know "honor" is not an animal, but I can think of an animal whose name begins with a silent "h".)
    Examples:

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had a pig, E-I-E-I-O.
    With an oink-oink here, and an oink-oink there.
    Here an oink, there an oink, everywhere an oink-oink.
    Old McDonald had a farm, E-I-E-I-O.

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had an owl, E-I-E-I-O.
    With a who-who here, and a who-who there.
    Here a who, there a who, everywhere a who-who.
    Old McDonald had a farm, E-I-E-I-O.

  • Update the verse function by creating "helper" function that will remove code repetition. For example, the first line and the last line of the verse could be printed by the same "helper" function. A significant place to reduce code duplication is in the a/an logic since that must appear 6 times in the verse function otherwise.

Notes about the challenge version:

  • There shall be only one verse function. Notice that the write-up states that the verse function is updated. Submissions with multiple verse functions shall not get credit for the challenge options.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def title():
print("The name of the song: "Old McDonald"")
print(" ")
  
def verse(animal, sound):
print("Old McDonald had a farm, E-I-E-I-O. And on that farm he had a "+ animal+" , E-I-E-I-O. With a "+ sound+" - "+sound+" here, and a "+ sound+" - "+sound+" there. Here a "+ sound+" , there a "+ sound+", everywhere a "+ sound+" - "+
sound+" . Old McDonald had a farm, E-I-E-I-O.")
print(" ")

def main():
title()
verse('chicken', 'cluck')
verse('cow', 'moo')
verse('duck', 'quack')

main()

showing indentation:

output:


Add a comment
Know the answer?
Add Answer to:
This assignment focuses on writing functions. These functions will be used to make the structure of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polym...

    C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight...

  • Suppose you are given the following production function Q= 21x + 9x2-x3 Compose a table calculating...

    Suppose you are given the following production function Q= 21x + 9x2-x3 Compose a table calculating the Total Product (Q), Marginal Product (MP) and Average Product (AP), for x ranging from 0 to 9 units. Using the information from the table, graph these individual curves and identify the rate of which diminishing marginal returns are evident. Delineate the stages of production and explain why Stages I and III are considered as irrational whereas Stage II is termed as rational.   What...

  • I HOPE TO SEE MY PILOT FACE TO FACE WHEN I HAVE CROSSED THE BAR England's...

    I HOPE TO SEE MY PILOT FACE TO FACE WHEN I HAVE CROSSED THE BAR England's great poet laureate was nearing the end of life. His time was running out, and he knew it. But he knew it with- out fear, and without regret. For to him death was not an end but a beginning; death was not the final curtain but merely a change of scene, the transition to another and perhaps an infinitely better life. Alfred Tennyson believed...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • This part of the Uber continuing case focuses on Chapter 14 and covers principles related to...

    This part of the Uber continuing case focuses on Chapter 14 and covers principles related to power, influence, and leadership. This continuing case's real-world application of management knowledge and skills is designed to help you develop critical thinking ability and realize the practical power of sound managerial skills for solving problems in your job and career. Read the continuing case and respond to the questions that follow. Situational approaches to leadership tell us that effective leadership depends on the situation...

  • Please, need help with this problem ! ( Cell Biology ) Thank You Problem 9 -...

    Please, need help with this problem ! ( Cell Biology ) Thank You Problem 9 - Panoptes In Ancient Greek mythology, the giant Argus was attributed the nickname "Panoptes" ("all seeing") because he had 100 eyes spread all over his head and body. And our friend Argus Panoptes, herem is afflicted with ubiquitous expression of the eyeless gene Aristotle and molecular biology Nowadays, researchers induce mutations in animal models to better understand mutations in humans In fruit flies, a genetic...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • Choose a symbol from either "The Gift of the Magi" and discuss what significance this symbol...

    Choose a symbol from either "The Gift of the Magi" and discuss what significance this symbol had to you as the reader (an example of this would be the combs Jim gave to Della and a discussion about a precious gift that you received or gave). Use text examples in this section. If someone were to write your life story, what symbol would be present and why?  How does this symbol represent you?   You can attach a picture of your symbol (an...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT