Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading