Home > Blockchain >  How to make script look for multiple of the same vowels in one string?
How to make script look for multiple of the same vowels in one string?

Time:09-03

I am trying to make a script that can count the number of vowels in a string, for example

String: sandwich
Number of vowels: 2

It works pretty well, except for one point. If I have a word with two or more of the same vowels, e.g. "cheese", the script counts it as one. Does anyone have a fix?

points = 0
word = input("Type a word. : ")
vowels = ["a", "e", "y", "u", "i", "o", "A", "E", "Y", "U", "I", "O"]
for char in vowels :
    if char in word:
      points  = 1
pointstr = str(points)
print(str("Vowels in word:"   pointstr))

Does anyone know how to fix this?

CodePudding user response:

Loop over the word instead of the vowels :)

points = 0
word = input("Type a word. : ")
vowels = ["a", "e", "y", "u", "i", "o", "A", "E", "Y", "U", "I", "O"]
for char in word:
    if char in vowels:
      points  = 1
pointstr = str(points)
print(str("Vowels in word:"   pointstr))

CodePudding user response:

Your code is almost correct. Instead of looping over the vowels first, loop over the words and check if that char is part of vowels.

Try this :

points = 0
word = input("Type a word. : ")
vowels = ["a", "e", "y", "u", "i", "o", "A", "E", "Y", "U", "I", "O"]
for char in word :
    if char in vowels:
      points  = 1
pointstr = str(points)
print(str("Vowels in word:"   pointstr))
  • Related