Write a function named twoWordsV2 that has the same specification as Problem 1, but implement it using while and not using break. (Hint: provide a different boolean condition for while.) Since only the implementation has changed, and not the specification, for a given input the output should be identical to the output in Problem 1.

Respuesta :

Answer:

I am writing a Python program. Here is the function twoWordsV2:

def twoWordsV2 (length,firstLetter):#definition of function which takes length of the first word and first letter of the second word as parameter and returns these two words in a list

   word1 = "" # stores the first word

   word2= "" #holds the second word  

   while(len(word1)!=length): # checks if the input word1 length is not equal to the specified length

       word1 = input('A ' + str(length) + '-letter word please: ') #asks user to enter the input word1 of specified length

   while(word2!=firstLetter): #checks if the first character of input word2 is not equal to the specified firstLetter character

       word2 = input('A word beginning with ' + firstLetter+ ' please: ')#asks user to enter the input word2 begining with specified first letter

       if word2[0] == firstLetter.upper() or word2[0] == firstLetter.lower():#second word may begin with either an upper or lower case instance of firstLetter      

           return [word1,word2] #return the two words in a list                

   

#to check the working of the function use the following statement        

print(twoWordsV2(4,'B')) #calls twoWordsV2  method by passing 4 (length) and B (first letter)

Explanation:  

twoWordsV2 method has two parameters i.e length which is the length of the first word and firstLetter which is the first character of the the second word. This means the first word should be of specified length, and the second word should begin with a specified first letter. The function returns the two resultant words with the above specifications. These two words are displayed in a list. I will explain the working of the function with the help of an example:

Suppose length = 4 and firstLetter = 'B' and user enters "ok" as word1. input() method takes first word input from user.

The first while loop checks if the length of word1 i.e. ok is not equal to specified length i.e. 4. Length of word1 is checked using len function which returns 2 so length of word1 is 2. The loop condition is true because length of word1 i.e. 2 is not equal to specified length i.e. 4. So the body of the loop executes which displays this message:

A 4-letter word please:

Here notice that str(length) is changed to 4. This is because length=4 and str() converts this value into string.

So the above message keeps displaying until user enters a four letter word. Lets suppose user now enters "four". Now the while loop condition evaluates to false and the loop breaks. The program control  moves to the next line which is a while loop.

The second while loop checks if the first character of input word2 is not equal to the specified firstLetter character. Suppose word2 = "apple" and firstLetter= 'B'

Now the loop condition evaluates to true because word2 is not equal to first letter B. Now the main part of this while loop is the if condition: if word2[0] == firstLetter.upper() or word2[0] == firstLetter.lower() This statement checks if the first index of the word2 is equal to firstLetter. The word2 can contain a capital B or a small B so if condition checks both the cases by using upper and lower methods that converts the firstLetter to upper or lower and then match with the first letter of word2 i.e. the letter at index 0 of the word2. If this condition evaluates to true then next return statement returns the word1 and word2 in a list otherwise the second while loop keep asking user to enter the word2 with starting letter to be firstLetter i.e. 'B'.

The screenshot of the program and its output is attached.

Ver imagen mahamnasir

The function for the given problem is:

  1. def twoWordsV2 (length,firstLetter):
  2. word1 = "" # stores the first word
  3. word2= "" #holds the second word  
  4. while(len(word1)!=length):
  5. word1 = input('A ' + str(length) + '-letter word please: ')
  6. while(word2!=firstLetter):
  7. word2 = input('A word beginning with ' + firstLetter+ ' please: ')
  8. if word2[0] == firstLetter.upper() or word2[0] == firstLetter.lower():    
  9. return [word1,word2]    
  10. print(twoWordsV2(4,'B'))

Brief Explanation:

These 10 lines of code were written using the Python programming language and this was used to define, store and hold the function and then request input from the user, check if the given input was according to the required parameters, and then print the output.

Read more about python programming language here:
https://brainly.com/question/7015522