Class

how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]

for element in how_many_s:
  if hasattr(element, "count"):
    print(element.count("s"))

I don’t know what is “attribute count” in this exercise

An attribute (in this case) is a method that you can use with a Python object. Some Python objects have the attribute “count”, and some do not.

print(hasattr(list, "count"))
print(hasattr(float, "count"))

my_list = [1,1,2,2,3,3]
print(my_list.count(3))

my_float = 3.14
print(my_float.count(3))

Output:

True    # list has the attribute count
False   # float does not
2          
Traceback (most recent call last):
  File "C:\path\to\test.py", line 10, in <module>
    print(my_float.count(3))
AttributeError: 'float' object has no attribute 'count'
2 Likes

thankkkkkkk :DDDDDDDDDDDDDDDDDDDDD

1 Like