The below code is returning 2. I think it should be returning 7. Guess it isn’t counting the string sassafrass at all?
Code:
how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]
for i in how_many_s:
count_s = 0
if hasattr(i, 'count'):
for x in i:
if x == 's':
count_s += 1
print(count_s)
```
@petercook0108566555
Happens with me too.
you just need to define count_s variable outside 1st for loop.
how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]
count_s = 0
for i in how_many_s:
if hasattr(i, 'count'):
for x in i:
if x == 's':
count_s += 1
print(count_s)
Cause of my English I didnt understand codecademy at all,
but thunks for you guys. I just solved the idea of MTF guru.
So the right answer for me was:
how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]
lst =[]
for i in how_many_s:
count_s = 0
if hasattr(i, 'count'):
for x in i:
if x == 's':
count_s += 1
lst.append(count_s)
print(lst)
You’re getting 2 because count_s is getting overwritten with each iteration for your first for loop. The 2 is the number of ‘s’ in the last iteration (the list). Move the count_s variable above the for loop and you should be set.