In Python, can you please help me solve this problem? ----- Paper Generator With
ID: 643649 • Letter: I
Question
In Python, can you please help me solve this problem?
-----
Paper Generator
With the end of the semester in (distant) sight, you're probably taking classes that require term papers. Wouldn't it be nice if a
computer could write those papers for you? We won't quite do that here (it's a really hard problem!), but let's make a first step in that
direction.
First, write functions called random_noun(), random_verb(), and random_adjective() that return a randomly-chosen noun, verb, or
adjective, respectively. (Include at least 5 nouns, 5 adjectives, and 5 verbs that the functions can choose from.) Then, write a function
called random_sentence() that returns a string of this form:
The (random_adjective) (random_noun) (random_verb) the (random_adjective) (random_noun).
with random words filled in using the corresponding functions you wrote above.
Finally, write a function called random_essay() that takes a single argument specifying the number of random sentences that the essay
should contain, and returns a string with that number of sentences created by calling random_sentence() repeatedly. Your main program
should ask the user for the number of sentences and print out the result. Here's an example:
>>>
How many sentences would you like in your essay? 3
Here's your automatically-generated essay:
The ugly sandwich sang the uneven dog.
The green flower ate the soggy tree.
The brilliant cat walked the brilliant sandwich.
Your functions should return strings instead of printing them. In other words, the only print statements in your program should be in the main body, not in the functions themselves.
Explanation / Answer
import random
def random_noun():
strings = ["sandwitch ", "dog ", "flower ", "tree ", "cat "]
r = random.randint(0, 4)
return strings[r]
def random_adjective():
strings = ["ugly ", "soggy ", "brilliant ", "uneven ", "green "]
r = random.randint(0, 4)
return strings[r]
def random_verb():
strings = ["sang ", "ate ", "walked ", "danced ", "played "]
r = random.randint(0, 4)
return strings[r]
def random_sentence():
return "The " + random_adjective() + random_noun() + random_verb() + "the " + random_adjective() + random_noun()
def main():
n = int(input('How many sentences would you like in your essay? '))
print('Here's your automatically-generated essay:')
for i in range(n):
print(random_sentence())
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.