Home > database >  How do you check if a string has something from the list and print it?
How do you check if a string has something from the list and print it?

Time:02-24

I'm trying to make a program that takes one word from the user and checks from a list of vowels and prints the vowels that are found in that word and how many were found. This is what I have so far, it's super incorrect but I tried something.

print("This program will count the total number of vowels in the word you enter.")

vowels = ["a", "e", "i", "o", "u"]
userWord = input("Please enter a single word: ")
if vowels in userWord:
    print(vowels)
    vowelCount = 0
    
    vowelCount = vowelCount   1
    
    print("There are "   vowelCount   " total vowels in "   userWord)

CodePudding user response:

Something you can do if you are starting out with Python is to iterate over every letter in the input word.

In each iteration you then check if the letter is in the vowel list like so:

print("This program will count the total number of vowels in the word you enter.")

vowels = ["a", "e", "i", "o", "u"]
userWord = input("Please enter a single word: ")
vowelCount = 0
foundVowels = []

for letter in userWord:
    if letter in vowels:
        foundVowels.append(letter)
        vowelCount  = 1
print("There are "   str(vowelCount)   " total vowels in "   userWord   ": "   str(foundVowels))

CodePudding user response:

Use the sum() function to count the number of vowels in the word.

vowelCount = sum(vowel in userWord for vowel in vowels)
print("There are {} total vowels in {}".format(vowelCount, userWord))

CodePudding user response:

vowels=['a','e','i','o','u']
count=0
ls=[]
wordfromUser=input("Enter a word: ")
for i in wordfromUser:
  if i in vowels:
    count =1
    ls.append(i)
print("There are "   str(count)   " total vowels in "   wordfromUser)
  • Related