'How to make definition that counts "e" in a word [closed]
I am trying to count the numbers of "e" in a word without using count.(). I made a definition but it outputs this:
0
1
1
2
How do I make it output just 2 ?
def letter_e(word):
count = 0
for letter in word:
if letter == "e" :
count += 1
print(count)
letter_e("fefe")
Solution 1:[1]
As your print statement is within your for loop, every time an "e" is found within the string your count variable is printed.
If you only want to print the final answer, print your count variable outside your for loop.
The code would look like this:
def letter_e(word):
count = 0
for letter in word:
if letter == "e" :
count += 1
print(count)
letter_e("fefe")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
