4 Replies - 3789 Views - Last Post: 26 November 2012 - 01:07 PM Rate Topic: -----

#1 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Finding the most occurring vowel in Python

Posted 10 November 2012 - 04:33 AM

I have a program that has to determine the most occuring vowel in a line of text but along with the number of occurrences as well if there 2 or more vowels that have the same number of iterations they should be both displayed.So far my program looks like this:
myString=input("Please enter your line of text: ")
vowels='aeiouAEIOU'
#we initialize a flag variable that will find the vowels with false
vowelfound=0
i=1
#we initialize the accumulator counters for vowels with 0
countA=countE=countI=countO=countU=0
for char in myString:
    if myString[i-1]=='a' or myString[i-1]=='A':
        vowelfound=1
        countA=countA+1
    elif myString[i-1]=='e' or myString[i-1]=='E':
        vowelfound=1
        countE=countE+1
    elif myString[i-1]=='i' or myString[i-1]=='I':
        vowelfound=1
        countI=countI+1
    elif myString[i-1]=='o' or myString[i-1]=='O':
        vowelfound=1
        countO=countO+1
    elif myString[i-1]=='u' or myString[i-1]=='U':
        vowelfound=1
        countU=countU+1

if vowelfound==0:
    print('There are no vowels in your line of text.')
else:
    if countA>countE:
        print('The most occuring vowel is A or a,with: ',countA,' occurrences')
    elif countE>countI:
        print('The most occuring vowel is E or e,with: ',countE,' occurrences')
    elif countI>countO:
        print('The most occuring vowel is I or i,with: ',countI,' occurrences')
    elif countI>countU:
        print('The most occuring vowel is E or e,with: ',countE,' occurrences')
    else:
        print('The most occuring vowel is U or u,with: ',countU,' occurrences')


But for example if my line of text is :Are you Bart,instead of displayind the ouput that the vowel 'a'has 2 occurences it gives the number 6,so 3 times more than the actual number of occurences.What I'm doig wrong?Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding the most occurring vowel in Python

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Finding the most occurring vowel in Python

Posted 10 November 2012 - 05:07 AM

You always check myString[i-1], and since i is always 1 (you never change it), that is always myString[0], or in your example, 'A'.

You are, however, already looping over the string with a for loop - why don't you simply check char instead?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: Finding the most occurring vowel in Python

Posted 10 November 2012 - 09:29 AM

I don't see that vowelfound=1 does anything for you. That "i" isn't really doing you any favors. The "vowels" variable, also not so much.

This logic doesn't work:
if countA>countE:
print('The most occuring vowel is A or a,with: ',countA,' occurrences')
[/code]
What if countI>countA?

You need to look at all counts to find the greatest. A list, rather than individual counters, would make more sense.

Personally, I'd start out with the follow list.
[[('A', 'a'), 0], [('E', 'e'), 0], [('I', 'i'), 0], [('O', 'o'), 0], [('U', 'u'), 0]]



However, if that makes little sense to you at this point, ignore it.
Was This Post Helpful? 1
  • +
  • -

#4 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Finding the most occurring vowel in Python

Posted 10 November 2012 - 12:30 PM

Yeah we just started learning about lists in my Python course,so if create that list and for every character in the given line of text a vowel it is found how do I increment that 0 to a 1 and so on. Because after than it only remains to compare the counters and displays the maximum values. Thanks again a lot .
Was This Post Helpful? 0
  • +
  • -

#5 irisbianca93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-November 12

Re: Finding the most occurring vowel in Python

Posted 26 November 2012 - 01:07 PM

Thank you, baavgai! I've used the idea that you've suggested to me and created tuples for storing the vowels it now it works perfectly :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1