0

i'm making a simple program for school - it should make a list of inputted sentences (which will be input until a sentence that has the the first and last symbol the same), then search for the most used vowel and use it to replace all the other vowels in all the inputted sentences (i haven't got to that part yet). But i have a problem, - i used python tutor for finding what's wrong as i got no error message directly in pyhon, and it seems it just won't let me count things inside lists and the "p" just remains at 0 for the whole time. I can't figure out what's wrong, so any help it appreciated! Sorry if it's just some rookie mistake, i'm quite new to python.

def V(sentence, vowel):
    a=0
    p=0
    b=""
    for i in sentence:
        for z in i:
            if z in vowel:
                p=sentence.count(z)
                if p>a:
                    a=p
                    b=z
    return b                
sentences=[]
vowels=["a", "e", "i", "o", "u", "y"]
v=input("input a sentence: ")
while v[0]!=v[-1]:
    sentences.append(v)
    v=input("input a sentence: ")
print("Most used vowel: ", V(sentences, vowels))
4
  • can you share what the output is and what you've tried so far? Commented Oct 13, 2017 at 15:17
  • 3
    Have you tried putting some print statements in to show the values of a, p and b each iteration... it'll fairly quickly show you what's up... Commented Oct 13, 2017 at 15:20
  • So far this is finding the most common vowel from each sentence, not all sentences. Is that what you want? Commented Oct 13, 2017 at 15:51
  • To find the most common vowel from all sentences, see my answer. Commented Oct 13, 2017 at 15:58

2 Answers 2

4

You're doing sentence.count(z) when you should be doing i.count(z). Your variable names are a little confusing. But sentence is the collection of sentences being passed, whereas i is the actual sentence.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this solved my problem! and sorry about the names, i had them in shortcuts of my native language so i just changed them when posting here
0

It is misleading to pass sentences as sentence and vowels as vowel.

To count in a list of strings to get the number of a vowel in that list is:

p = 0               # 1
for s in sentences: # 1
    p += s.count(z) # 1

Or if you are a fan of one-liners:

p = sum([s.count(z) for s in sentences]) # 2

Instead of doing:

for i in sentence:
    for z in i:
        # ...

You should loop through vowels, so you only count each vowel once:

for z in vowels:
    p = 0               # 1
    for s in sentences: # 1
        p += s.count(z) # 1
    if p > a:
        a = p
        b = z

Cleaning up, that gives:

def mostUsedVowel(sentences, vowels):
    a = 0
    p = 0
    b = ""
    for vowel in vowels:
        p = 0                   # 1
        for s in sentences:     # 1
            p += s.count(vowel) # 1
        if p > a:
            a = p
            b = vowel
    return b

You can replace # 1 with # 2 if you prefer - just remember to replace z with vowel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.