In: Computer Science
Python. Write a code that asks the user to enter a string.
Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total.
You may need to write 5 different if statements, one for each vowel.
Enter a string: mouse
mouse has 3 different vowels
In this Python code,
- We are prompting the user to enter the string.
- Then we are using a for loop and 5 if conditions to match the
vowel and incrementing the count.
- Finally, we are printing the vowel count to the console.
NOTE: Please Look into the screenshot for Indentation. This compiler will
remove all the indentation which is mentioned in the code.
(Feel free to drop me a comment, If you need any help)
Code:
string=input("Enter a string:")
vowelCount=0 #To store the count of vowels
#Since we are accepting a string not a single char
#we need to check for each and every character in the string
#and then increment the vowel count using a for loop
#and then using 5 if statements one for each vowel
for i in string:
if i=='a':
vowelCount=vowelCount+1
if i=='e':
vowelCount=vowelCount+1
if i=='i':
vowelCount=vowelCount+1
if i=='o':
vowelCount=vowelCount+1
if i=='u':
vowelCount=vowelCount+1
print(string,"has",vowelCount,"different vowels")
Please check the
compiled program and its output for your reference:
Output:
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...