Dec-30-2020, 05:18 PM
Hi,
I'm moving from a C background to python. I'm trying to count the number of vowels in a string. I've put the vowels in a list, and I'm trying to see if the list's 1st vowel is found at a string position. If true, it logs it. If false, the string position is increased. This process continues until the string position matches the length of the string.
After this, the next vowel in the list is used, and the process starts over again.
This continues until all vowels have been tried.
In running the code, it only returns 0, so I'm not sure where my logic is going wrong.
Note that I had thought about using the string method, rindex(value, start, end), and then increasing the start and end values by 1 each pass (where they begin at start = 0, end = 1), and using the value as a reference to the vowel list, but I couldn't exclude the error handling of when a vowel wasn't found (i.e. pass over it).
Thoughts?
I'm moving from a C background to python. I'm trying to count the number of vowels in a string. I've put the vowels in a list, and I'm trying to see if the list's 1st vowel is found at a string position. If true, it logs it. If false, the string position is increased. This process continues until the string position matches the length of the string.
After this, the next vowel in the list is used, and the process starts over again.
This continues until all vowels have been tried.
In running the code, it only returns 0, so I'm not sure where my logic is going wrong.
Note that I had thought about using the string method, rindex(value, start, end), and then increasing the start and end values by 1 each pass (where they begin at start = 0, end = 1), and using the value as a reference to the vowel list, but I couldn't exclude the error handling of when a vowel wasn't found (i.e. pass over it).
Thoughts?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
txt = "Count the number of vowels in this sentence." #format: txt.rindex(value, start, end) txt = [ "a" , "e" , "i" , "o" , "u" ] #vowels to test for element = 0 counter = 0 position = 0 while position < len (txt) and element < len (txt): if txt[element] = = txt[position] and position < len (txt): print ( "vowel counter = " , counter) position + = 1 # inc list index position by 1 else : # go to next vowel to search for # reset position to 0 to next vowel can be search for element + = 1 position = 0 print ( "Search complete, total vowels found: " , counter) |