Ask Question
2 September, 13:26

Write a"" rhyme""/"" alliteration"" program to use Boolean methods to test for rhyming and alliteration. (#rhyme: return true if s1 and s2 end with the same two letters #alliterate return true if s1 and s2 start with the same letter Sample output: Type two words: bare blare (input from user) They rhyme! They alliterate!

+1
Answers (1)
  1. 2 September, 15:57
    0
    def rhyme_alliterate (s1, s2):

    if not s1. isalpha () or not s2. isalpha ():

    raise ValueError ('Only strings of alphabets are allowed')

    s1_first_letter = s1[0]. lower ()

    s1_last_letter = s1[-1]. lower ()

    s2_first_letter = s2[0]. lower ()

    s2_last_letter = s2[-1]. lower ()

    if s1_first_letter = = s2_first_letter and s1_last_letter = = s2_last_letter:

    return 'They rhyme! They alliterate!'

    elif s1_first_letter = = s2_first_letter:

    return 'They alliterate!'

    elif s1_last_letter = = s2_last_letter:

    return 'They rhyme!'

    else:

    return 'No rhyme! No alliterate!'

    word1 = input ('Enter the first word: ')

    word2 = input ('Enter the second word: ')

    print (rhyme_alliterate (word1, word2))

    Explanation:

    The above snippet is written in Python Programming Language. A function called rhyme_alliterate is defined with two arguments which are of type string. These strings must contain only alphabets and this is the validation we are enforcing with this line of code:

    if not s1. isalpha () or not s2. isalpha ():

    If a non-alpha string is passed in by the user, a valueError is raised.

    The line of code s1_first_letter = s1[0]. lower () is simply assigning the first value of the first word to a variable s1_first_letter and the lower method transforms the value to lower case. The reason for this is to make the comparison case insensitive.

    The first conditional statement after validation is passed simply checks if the first and last letters of both words tally, if they do, 'They rhyme! They alliterate!' is returned

    However, if just the first letter of both words tally, 'They alliterate!' is returned and if the last letter of both words tally, 'They rhyme!' is returned.

    'No rhyme! No alliterate!' is returned otherwise
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a"" rhyme""/"" alliteration"" program to use Boolean methods to test for rhyming and alliteration. (#rhyme: return true if s1 and s2 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers