Hey all, in this amazing tutorial, we are going WAP a program in Python to find the number of vowels in a sentence. So be ready with your code editors as we are going to do this program in the best way possible!
As always, you can find the final code at the end of this article. But, I would recommend you to be with us this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world!
Overview:
In this program, we are asked to find the number of vowels in a sentence. But first, what is a vowel? Vowels in English language are such such sounds (letters) that can be produced without using other sounds. For fun, try to pronounce the consonants (non-vowels) without using vowels. And believe me, you can't!
Vowels in English Language are: a, e, i, o and u
So, first we would ask the user for the input. Then we would make a list of these vowels and then run a for loop of the given input to check for each each and every letter for the input. If any letter in the input matches with a vowel, then we would increase the value of the variable 'count' by 1. After the loop ends, we would print 'count' to the user.
*As I have already told you the basics of the program, you can try to do it on your own. Check if your solution matches with ours!*
Understanding what we need to do:
Here, I will tell you step-by-step what you need to do.
Step 1: Asking the user for the input
Now, our first step would be to ask the user for the input. I have stored its value in the variable named 'sentence'. Also, with common sense, we could say that the sentence entered by the user may or may not have vowels in same case. Hence to avoid checking for both lower and upper cases, we can convert the sentence entered by the user in a particular case.
This time we are going with the lower case, but you can convert it in upper case if you want. To do this, I have made a variable 'string', that is equal to sentence.lower(). I hope you understood.
*Note that, if you go with the upper case, you can use the sentence.upper() command. Also, for that you need to change the value of the list (that we are going to make in the Step2) from lower case vowels to upper case vowels.*
Here's the code:
Step2: Finding the number of vowels in a sentence
Now that we have converted the user input into a lower case, we can move on to the rest of the program. Now we have to make a list (I named the variable as list1), that stores the vowels. I have also declared a variable called 'count' that will store the count of number of vowels.
Now in the for loop, we need to check every character in the new 'string' variable. To do that, in the for command itself, I set a variable called 'char' that is defined to get the value of each letter of the input.
Next, we would write an if condition to check if that 'char' matches with any value in the 'list1' i.e., the list that contains the vowels. Each time the condition satisfies, we would iterate the value of 'count' by 1. After the loop ends, we print the value of 'count' to the user. I hope you understood.
Here's the code:
So, we have successfully found the best way to WAP (Python) to find the number of vowels in a sentence!
Here's the final code:
So this was the final code. I hope that you have learnt something new and if you did then don't forget to share it someone in need. This many help somebody struggling in this program.
Also read: WAP (Python) to find whether the number entered by the user is a prime number or not
Please subscribe to our newsletter as we do these coding sessions each week!
Again, thanks for reading!
No comments:
Post a Comment