-2

Please help me to solve this!

Question: Define a function named contains_all_the_vowels(x) which returns whether x contains all the vowels (a, e, i, o, u) or not

Here's my code:

def contains_all_the_vowels(x):
vo= 'aeiou'
if vo in x:
    return True
else:
    return False

And it shows:

AssertionError: None is not true
Luz
  • 7
  • 1
  • 3

3 Answers3

0

Not pretty but simple.

def contains_all_the_vowels(x):
    if "a" in x and "e" in x and "i" in x and "o" in x and "u" in x:
        return True
    return False
zerecees
  • 697
  • 4
  • 13
0

You are searcing for the whole word aeiou instead of each character. Use this instead :

def contains_all_the_vowels(x):
    vowels = ['a','e','i','o','u']
    for v in vowels:
        if v not in x:
            return False
    return True
DeGo
  • 783
  • 6
  • 14
  • You can also do `vowels = 'aeiou'` – altermetax Apr 17 '21 at 18:35
  • Thanks! One more question, what should I do if I want to check whether if contains one of the vowels or not? – Luz Apr 17 '21 at 18:38
  • Thanks for the tip @altermetax. I went with the list to explicitly show that one is iterating through individual characters. – DeGo Apr 17 '21 at 18:38
  • @夏丸子 While iterating, if you encounter even one vowel, return `True` instead – DeGo Apr 17 '21 at 18:38
  • If you want "any", "all", or "none" you need to return more than True or False, so I think you'd be better served to edit your question showing how you want to handle three return cases instead of two. – labroid Apr 17 '21 at 18:57
  • 1
    This solution is incorrect. It returns False on the first vowel found. I think you meant `if v not in x:` shown in my answer. As a side note, the comprehension method of my answer should both be faster and allow easy 'any', 'all', and 'none' methods. – labroid Apr 17 '21 at 19:06
  • Thanks to all of you ! – Luz Apr 17 '21 at 19:20
0

Membership checks see if the argument on the left is in the array on the right. So "a" in "abc" checks if the character "a" is in "abc". while "abc" in "string" checks if the substring "abc" appears in "string"

To solve your problem, the verbose method would be

def contains_all_the_vowels(x):
  vo = 'aeiou'
  for c in vo:
    if c not in x:
      return False
  return True

using a list comprehension it may be written

def contains_all_the_vowels(x):
    return all([c in x for c in 'aeiou'])

The handy thing about using the list comprehension form is that it is easy to handle the "any vowels" or "all vowels" or "no vowels" cases like this:

def contains_all_the_vowels(x):
    occurs = [c in x for c in 'aeiou']
    if all(occurs):
        return "All vowels found"
    if any(occurs):
        return "At least one vowel found"
    return "No vowels found"
labroid
  • 452
  • 4
  • 14