'input paragraph and word from user, print words with same vowels as word from paragraph

this is given question img

I tried this question with regular expression ,but it doesn't provide me with required output



Solution 1:[1]

The only situation in which a word isn't in the output list is if a vowel is in the input word but not in the word you are checking. You can use this fact like so:

def vowels_match(word, input_word):
    for vowel in ['a', 'e', 'i', 'o', 'u']:
        if vowel in input_word and not vowel in word:
            return False
    return True

paragraph = input('The paragraph is: ').split()
paragraph = [''.join([letter for letter in word if letter.isalpha()]) for word in paragraph]

while True:
    input_word = input('Input Word: ')
    print([word for word in paragraph if vowels_match(word, input_word)])

This outputs:

The paragraph is: It is a beautiful and lovely day today.
Input Word: and
['a', 'beautiful', 'and', 'day', 'today']
Input Word: peace
['beautiful']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1