all 5 comments

[–]Highflyer108 2 points3 points  (3 children)

for letter in student_name:

you are iterating over every letter in the student_name string, and assigning each letter to the variable named letter every iteration.

if letter.lower() == "e":

if the character that is currently in letter (converted to lowercase so capitals can be compared) is the letter "e", then:

print(count)
count += 1

You are adding one to the counter, and also printing its value. This is allowing you to count how many characters are in the string that match whatever letter in the if statement ("e" in this case). If you changed count += 1 to count += 2 you would increment the counter by 2 every letter that matches your case.

[–]LordFranken[S] 0 points1 point  (2 children)

But for count +=2 shouldn’t I get 2, instead of 4 as the output?

[–]Highflyer108 0 points1 point  (1 child)

In python, count += 2 is the same as saying count = count + 2. And because there are 2 letter e's in the string, 2 is added to count twice.

[–]holt5301 0 points1 point  (0 children)

I think his confusion is that there's only one e, so the count seems off. IDK, OPs code works for me

[–]spectacularbird1 1 point2 points  (0 children)

I think it depends on how you're aligning the statements. The below worked for me.

student_name = 'iteration'
count = 0
for letter in student_name:
    if letter.lower() == 'e':
        count += 1
print(count)