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?
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 .
Get Answers For Free
Most questions answered within 1 hours.