0

I hope I may be able to get some help. The point of this program is that it is supposed to take a count of the vowels in a sentence given by the user. I cannot get it to increase the count and everything is staying at zero. I was hoping someone may be able to help show me where I may have messed up the code. It has to stay in a very similar format with the for loop iterating directly over the string. I've tried manipulating it so many different ways. If anyone is able to help I am posting a the code that I have. Thanks!

VOWELS = 'AEIOU' def count3(string, letter): count = 0 # for loop only loops over index, don't initialize or incriminate index for char in string: #letters = string[char] letter_low = str.lower(letter) if char == letter_low: count = count + 1 return (letter + " : %d" % count) # come back to this, not increasing count of each vowel def main(): print("Enter a sentence and this sentence will display its vowel count.") sent = input("Enter the sentence to be analyzed: ") while sent: print("Your sentence was: " + sent) sent_low = str.lower(sent) print("\nAnalysis") for letter in VOWELS: print(count3(sent_low, letter)) 
4
  • 1
    I think you want to have your for loop inside your main function. Also, I think that the while loop is going to be infinite, meaning you won't even ever enter the for letter in VOWELS loop, and thus won't ever call your count3 function. Commented Apr 8, 2021 at 17:48
  • Wait you just want to find how many vowels are in a sentence? Commented Apr 8, 2021 at 17:50
  • The for loop is inside the main function right now, sorry, this is my first time posting and it wouldn't let me just post a pic so the code was all indented incorrectly. Commented Apr 8, 2021 at 17:53
  • Yes, I need to find how many vowels are in a sentence, without just calling the count function. Commented Apr 8, 2021 at 17:53

3 Answers 3

1

I think your code could use a little cleaning up.

def count3(string): vowels = ['a','e','i','o','u'] count = 0 for char in string: if char in vowels: count += 1 return count def main(): sent = input("Enter a sentence and this sentence will display its vowel count: ") print("Your sentence was: " + sent) sent_low = sent.lower() vowels = count3(sent_low) print(f"Your string has {vowels} number of vowels") main() 
Sign up to request clarification or add additional context in comments.

2 Comments

That could work, but I guess I should have mentioned that it needed to have the argument, count3(string, letter). Also for the if statement it needs to be char == letter which then adds to the count.
Why does it need to be char==letter? Also what would letter be?
0

What if you used the .count() method?

def count3(string): count = 0 vowels = ['a', 'e', 'i', 'o', 'u'] string_as_list = list(string.lower()) for vowel in vowels: count += string_as_list.count(vowel) return count def main(): while True: print("Enter a sentence and this sentence will display its vowel count.") sent = input("Enter the sentence to be analyzed: ") print(count3(sent)) return None main() 

1 Comment

Unfortunately, I cannot use the .count() method. I need to write a function that does the counting. It has to be a for loop iterating directly in the string. The count3 function must also be def count3(string, letter):
0

Your code works for me after I remove the while statement, which was causing an infinite loop.

# Exactly the same as your code, with the while statement removed VOWELS = 'AEIOU' def count3(string, letter): count = 0 # for loop only loops over index, don't initialize or incriminate index for char in string: # letters = string[char] letter_low = str.lower(letter) if char == letter_low: count = count + 1 return (letter + " : %d" % count) # come back to this, not increasing count of each vowel def main(): print("Enter a sentence and this sentence will display its vowel count.") sent = input("Enter the sentence to be analyzed: ") print("Your sentence was: " + sent) sent_low = str.lower(sent) print("\nAnalysis") for letter in VOWELS: print(count3(sent_low, letter)) ### Example output if I run main() and enter "this is a test string" # Analysis # A : 1 # E : 1 # I : 3 # O : 0 # U : 0 

HOWEVER... There are better ways to do what you're trying to accomplish. For example, here's how you could leverage the .count() method. This produces the same result as your code while eliminating the for loop.

def count_occurrences(string, letter): return string.lower().count(letter.lower()) def main(): print("Enter a sentence and this sentence will display its vowel count.") sent = input("Enter the sentence to be analyzed: ") print("Your sentence was: " + sent) for letter in VOWELS: count = count_occurrences(sent, letter) print(f"{letter}: {count}") 

If you insist on not using the .count() method, you can still simplify your code. For example:

def count_occurrences(string, letter): return len([x for x in string if x.lower() == letter.lower()]) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.