Reputation: 1
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))
Upvotes: 0
Views: 77
Reputation: 187
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()])
Upvotes: 0
Reputation: 1284
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()
Upvotes: 0
Reputation: 5889
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()
Upvotes: 1