will thompson
will thompson

Reputation: 11

How do I display what specific vowels are in the word

I have finished almost everything in my homework program. The last feature is getting the program to display the specific vowels that were found in the input. For example:

Please enter a word: look
The vowels in your word are 
o
o
there were 2 vowels
I'm terribly sorry if I missed any 'y's.

Code:

def main():
    vowels = ["a","e","i","o","u"]
    count = 0
    string = input(str("please enter a word:"))
    for i in string:
        if i in vowels:
            count += 1

    print("The vowels in your word are:")

    print("There were",count,"vowels")
    print("Sorry if I missed any 'y's")

if __name__ == "__main__":
    main()

Upvotes: 0

Views: 81

Answers (2)

toonarmycaptain
toonarmycaptain

Reputation: 2331

You can put a print statement within your if. This way when a vowel is found, it is printed in the manner you displayed in your question.

NB you'll need to move your print("The vowels in your word are:") to before your if so that it is printed before the vowels.

eg

def main():
    vowels = ["a","e","i","o","u"]
    count = 0
    string = input(str("please enter a word:"))
    print("The vowels in your word are:") #puts text before the vowels printed in `if`
    for i in string:
        if i in vowels:
            count += 1
            print (i) #prints the vowel if it is found



    print("There were",count,"vowels")
    print("Sorry if I missed any 'y's")

if __name__ == "__main__":
main()

Upvotes: 0

Prune
Prune

Reputation: 77885

All you're missing is to keep a string of the vowels as you find them. This is a lot like counting the vowels. Start it at the "base value" of a string, the empty string. Every time you find a vowel, add (concatenate it) to your string. For instance:

vowels_found = ""
for i in string:
    if i in vowels:
        vowels_found += i
print(vowels_found)
print(len(vowels_found))

After this, print vowels_found just where you planned. If you want them on separate lines, as in your posted sample, then print each one inside the loop, and don't use vowels_found at all.

There are more advanced, more direct ways to do this in python: you can include the filtering in a single statement such that this routine is basically two lines long: one to collect the vowels, the other to count and print them. Worry about those a little later in the class ... but if someone posts those solutions, pay attention. :-)

Upvotes: 2

Related Questions