Question

Nested Loops Problem 3 Write a function called makesentence() that has three parameters: nouns, verbs, and...

Nested Loops

Problem 3

Write a function called makesentence() that has three parameters: nouns, verbs, and gerunds. Each parameter is a list of strings, where nouns list has noun strings (such as 'homework'), verbs list has veb strings (such as 'enjoy'), and gerunds list has gerund strings (those -ing words, such as 'studying'). The function will go through all these lists in a systematic fashion to create a list of all possible sentences that use all the noun, verb, and gerund strings. The function then returns the list of sentences.

Each sentence has the following format: 'I' followed by a verb, followed by a noun, followed by 'when ', followed by a gerund, exactly in this order.. For example: 'I enjoy homework when studying' uses the noun 'homework', verb 'enjoy', and gerund 'studying'.

Consider that we have the following lists:

some_nouns = ['homework', 'music', 'breaks']

some_verbs = ['enjoy', 'ignore']

some_gerunds = ['studying', 'sleeping', 'hiking']

After we write the function definition, we call the function, save its return value in the variable result, and print out the result:

result = makesentence(some_nouns, some_verbs, some_gerund)

print(result)

The output of obtained from the call above is:

['I enjoy homework when studying', 'I enjoy homework when sleeping', 'I enjoy homework when hiking', 'I enjoy music when studying', 'I enjoy music when sleeping', 'I enjoy music when hiking', 'I enjoy breaks when studying', 'I enjoy breaks when sleeping', 'I enjoy breaks when hiking', 'I ignore homework when studying', 'I ignore homework when sleeping', 'I ignore homework when hiking', 'I ignore music when studying', 'I ignore music when sleeping', 'I ignore music when hiking', 'I ignore breaks when studying', 'I ignore breaks when sleeping', 'I ignore breaks when hiking']

Part A Think about how to solve this problem and the computational steps of your solution BEFORE you write the Python code. Explain (1) what computations you’ll instruct Python and (2) why. Write your explanations in the order in which the computations should occur.

Part B

#1 Using the explanations of the computational steps of your solution, in an Active Code scratchpad, write the implementation. The implementation has the following structure: your function definition, followed by the three list assignments, followed by the function call and print() call, as follows:

[ your function definition goes in here ]

some_nouns = ['homework', 'music', 'breaks']

some_verbs = ['enjoy', 'ignore']

some_gerunds = ['studying', 'sleeping', 'hiking']

result = makesentence(some_nouns, some_verbs, some_gerund)

print(result)

Copy and paste the code of your function definition

Copy and paste the output you get when you run & save your code

Part C

#1 What was the most challenging while solving Problem 3?

#2 Why?

Homework Answers

Answer #1

def makesentence(some_nouns,some_verbs,some_gerunds):
result=[];
for i in some_verbs:
for j in some_nouns:
for k in some_gerunds:
result.append("I "+i+" "+j+" while "+k);
return result;
  


  

some_nouns = ['homework', 'music', 'breaks']

some_verbs = ['enjoy', 'ignore']

some_gerunds = ['studying', 'sleeping', 'hiking']

result = makesentence(some_nouns, some_verbs, some_gerunds)

print(result)

code indentation screenshot:

Expected output:

#1.The complex part was usage of nested loops to get the desired output.

#2.Because nested loops are quite tricky so it was the challenging part .

Know the answer?
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
Similar Questions
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings....
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for us: "Elvis" and "Lives" would be considered #anagrams. So would "Eleven plus...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT