W02 Prove: Writing Functions
Purpose
Prove that you can write functions with parameters and call those functions multiple times with arguments.
Problem Statement
In English, a preposition is a word used to express spatial or temporal relations, such as “in”, “over”, and “before”. A prepositional phrase is group of words that begins with a preposition and includes a noun. For example:
above the water
in the kitchen
after the meeting
Assignment
Write the second half of a Python program that generates simple English sentences that you began in the prove milestone. As part of the previous lesson’s prove milestone, you wrote a program that generates English sentences with three parts: a determiner, a noun, and a verb. During this prove assignment, you will add functions so that your program generates sentences with four parts:
-
a determiner
-
a noun
-
a verb
-
a prepositional phrase
For example:
One girl talked for the car.
A bird drinks off one child.
The child will run on the car.
Some dogs drank above many rabbits.
Some children laugh at many dogs.
Some rabbits will talk about some cats.
To complete this prove assignment, your program must include at least these seven functions:
-
main
-
make_sentence
-
get_determiner
-
get_noun
-
get_verb
-
get_preposition
-
get_prepositional_phrase
You may add other functions if you find them helpful. The
get_preposition
function must randomly choose a
preposition from a list and return the randomly chosen preposition.
The get_prepositional_phrase
function must make a
prepositional phrase by calling the get_preposition
,
get_determiner
, and get_noun
functions.
Helpful Documentation
-
The preparation content for week 01 explains how to call functions.
-
The preparation 1 content for this lesson explains how to write functions.
-
The preparation content 2 for this lesson explains variable scope and good function design.
Steps
Do the following:
-
Use the
get_determiner
function from the previous lesson’s prove milestone as an example to help you write theget_preposition
function. Theget_preposition
function must have the following header and fulfill the requirements of the following documentation string.def get_preposition(): """Return a randomly chosen preposition from this list of prepositions: "about", "above", "across", "after", "along", "around", "at", "before", "behind", "below", "beyond", "by", "despite", "except", "for", "from", "in", "into", "near", "of", "off", "on", "onto", "out", "over", "past", "to", "under", "with", "without" Return: a randomly chosen preposition. """
-
Write the
get_prepositional_phrase
function to have the following header and fulfill the requirements of the following documentation string.def get_prepositional_phrase(quantity): """Build and return a prepositional phrase composed of three words: a preposition, a determiner, and a noun by calling the get_preposition, get_determiner, and get_noun functions. Parameter quantity: an integer that determines if the determiner and noun in the prepositional phrase returned from this function should be single or pluaral. Return: a prepositional phrase. """
-
Add code to the
make_sentence
function and write any other functions that you think are necessary for your program to generate and print six sentences, each with a determiner, a noun, a verb, and a prepositional phrase. The six sentences must have the following characteristics:Quantity Verb Tense a. single past b. single present c. single future d. plural past e. plural present f. plural future
Call Graph
The following call graph shows the user-defined functions and
function calls and returns as you should write them in your
sentences.py
program. From this call graph we see the
following function calls:
-
The computer starts executing the
sentences.py
program by calling themain
function. -
While executing the
main
function, the computer calls themake_sentence
function. -
While executing the
make_sentence
function, the computer calls theget_determiner
,get_noun
,get_verb
, andget_prepositional_phrase
functions. -
While executing the
get_prepositional_phrase
function, the computer calls theget_preposition
,get_determiner
, andget_noun
functions. -
While executing each of the
get_determiner
,get_noun
,get_verb
, andget_preposition
functions, the computer calls therandom.choice
function. -
Then, the computer executes the
str.capitalize
method. -
Finally, the computer executes the
print
function.
Testing Procedure
Verify that your test program works correctly by following each step in this procedure:
-
Run your
sentences.py
program and ensure that your program’s output is similar to the sample run output shown here. Because your program randomly chooses the determiners, nouns, verbs, and prepositions, your program will generate different sentences than the ones shown here.> python sentences.py One girl talked for the car. Some dogs drank above many rabbits. One bird drinks off one child. Some children laugh at many dogs. The child will run on the car. Some rabbits will talk about some cats.
Exceeding the Requirements
If your program fulfills the requirements for this assignment as described in the previous prove milestone and the Assignment section above, your program will earn 93% of the possible points. In order to earn the remaining 7% of points, you will need to add one or more features to your program so that it exceeds the requirements. Here are a few suggestions for additional features that you could add to your program if you wish.
-
Within your
make_sentence
function add another call toget_prepositional_phrase
so that each sentence includes two prepositional phrases like this:One girl across one cat talked for the car.
A bird near the rabbit drinks off one child.
The child under the cat will run on the car.
Some dogs without a cat drank above many rabbits.
Some children from a bird laugh at many dogs.
Some rabbits behind one man will talk about some cats. -
Write a function named
get_adjective
and call it in yourmake_sentence
function to add an adjective to the sentences produced by your program. Does it make sense to callget_adjective
in yourget_prepositional_phrase
function? -
Write a function named
get_adverb
and call it in yourmake_sentence
function to add an adverb to the sentences produced by your program.
Ponder
What changes would you have to make to your program so that it could produce sentences that fit the following form?
{Determiner} {adjective} {noun} {prepositional_phrase} {adverb} {verb} {determiner} {adjective} {noun} {prepositional_phrase}.
Such as these sentences:
The red birds in the air quickly ate some fast fish in the water.
The busy bird with a car sweetly drank many smart rabbits by one boy.
One dinky boy near the dog calmly grew some tall children across a cat.
Submission
To submit your program, return to Canvas and do these two things:
-
Upload your
sentences.py
file for feedback. -
Add a submission comment that specifies the grading category that best describes your program along with a one or two sentence justification for your choice. The grading criteria are:
-
Some attempt made
-
Developing but significantly deficient
-
Slightly deficient
-
Meets requirements
-
Exceeds requirements
-
Up Next
- W02 Ponder: Progress Reflections
Other Links:
- Return to: Week Overview | Course Home