Question: 5 . Consider the following code: 1 # Read in words from the user, and 'clean' it for later use 2 noun = input (

5. Consider the following code:
1 # Read in words from the user, and 'clean' it for later use
2 noun = input("Please enter a (non-proper) noun: ")
3 noun = noun.lower() # Make lowercase
4 noun = noun.strip() # Get rid of extra whitespace
5
6 adj = input("Please enter an adjective:" )
7 adj = adj.lower() # Make lowercase
8 adj = adj.strip() # Get rid of extra whitespace
9
10 verb = input("Please enter a verb:" )
11 verb = verb.lower() # Make lowercase
12 verb = verb.strip() # Get rid of extra whitespace
13
14 adv = input("Please enter an adverb:" )
15 adv = adv.lower() # Make lowercase
16 adv = adv.strip() # Get rid of extra whitespace
17
18 print(f "The { noun} was a very { adj}{ noun}. It always { adv}{ verb} everywhere." )
19
(a) As we can see, it is very repetitive. What is a function you could write to simplify the
code?
(b) What are some benefits of putting the code into a function, instead of leaving it as is?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!