Ask Question

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!

+4
Answers (1)
  1. 5 August, 01:05
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers