1

I'm trying to check if the user input contains a vowel or not. However, I've only found how to check for one vowel at a time, but not all.

vowel = ("a")

word = input("type a word: ")

if vowel in word:
 print (f"There is the vowel {vowel} in your word")
else:
 print ("There is no vowel in your word")

This seems to work but I get a error if I try to make the vowel variable into a list. ["a","e","i","o","u"]

any ideas how to check for e i o u at the same time?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50

6 Answers6

2

If you do not need to know which vowels are present, you can use any as follows.

vowels = ("a", "e", "i", "o", "u")

word = input("type a word: ")

if any(v in word for v in vowels):
    print("There is at least one vowel in your word.")
else:
    print("There is no vowel in your word.")
LeopardShark
  • 3,820
  • 2
  • 19
  • 33
1

One way to keep track is also to have an existence list that keeps all vowels that exist in a word.

existence = []
vowels = ["a","e","i","o","u"]
test_word = "hello" # You can change this to receive input from user

for char in  test_word:
    if char in vowels:
        existence.append(char)
if existence and len(existence) > 0:
    for char in existence:
        print(f"These vowels exist in your input {test_word} - {char}")
else:
     print(f"There are no vowels existing in your input {test_word}")

Output:

These vowels exist in your input hello - e
These vowels exist in your input hello - o
coldy
  • 2,115
  • 2
  • 17
  • 28
  • 1
    I find this code a bit excessive for this task. for one, you pick every letter in the search string (N), instead of picking every vowel (5). in the word `hello`, you'd check twice if `l` is among the vowels. it'd be less work to iterate over the vowels and check if each is in the search string. -- further, the condition `existence and len(existence) > 0` is redundant, since you initialized it to be a list already. either side of the `and` suffices here. – Christoph Rackwitz Mar 05 '22 at 21:40
  • 1
    Thanks for the input and suggestions for improvement :). Ideally, I could have returned True from `if char in vowels:` when a vowel is present. However, by looking at the code of the OP I thought there is a need to also know which all vowels could be present within the word, hence the approach. I agree on the second part of the comment fully. – coldy Mar 06 '22 at 10:57
  • `for vowel in vowels: if vowel in word: print(vowel, "in word")` (the "simple" variant) -- `found = [vowel for vowel in vowels if vowel in word]; if found: print("found vowels:", found); else: print("no vowels found")` (the "complex" variant) -- that'd also elucidate which vowels are contained, while swapping what letter is looked up in what string. – Christoph Rackwitz Mar 06 '22 at 13:39
1

A regular expression can tell you not only if there's a vowel in a string, but which vowels and their order.

>>> import re
>>> re.findall('[aeiou]', 'hello')
['e', 'o']
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

I can solve your problem. Here is the code:

vowels = {'a','e','i','o','u'}

word = input("Enter a word: ")

for vowel in word:

  if vowel in vowels:
    print(vowel,"is a vowel")
  • 1
    You need to format your code properly. Also, code only answers are generally discouraged. Answers should contain an explanation of the problem or solution. – Ted Klein Bergman Feb 14 '22 at 13:47
0

I feel that if you want to use only if statements, then you can only choose one of the vowels but if you wish to use the for and if statements, it can go through with the whole vowels

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Roxy Nov 16 '22 at 08:52
-1

you have to iterate over the list.

vowels =  ["a","e","i","o","u"]

word = input("type a word: ")

for vowel in vowels:
  if vowel in word:
     print (f"There is the vowel {vowel} in your word")
  else:
     print ("There is no vowel in your word")

iteration is the proces where you go through each item in a list.

for example.

list_a = ['a', 'b', 'c' ]

for item in list_a:
  print(item)

#output will be a b c 

since other user complained in a comment. If you want to stop the loop after vowel found, you should add break statment

vowels =  ["a","e","i","o","u"]

word = input("type a word: ")

for vowel in vowels:
  if vowel in word:
     print (f"There is the vowel {vowel} in your word")
     break
  else:
     print ("There is no vowel in your word")
Mladen Milosavljevic
  • 1,720
  • 1
  • 12
  • 23