Cannot get past step 1 in Introduction to classes

Can anybody tell me what I’m doing wrong here, please?

Link to the lesson

Introduction to classes

Screenshot

Instructions

For every element in can_we_count_it, check if the element has the attribute .count using the hasattr() function. If so, print the following line of code:

print(str(type(element)) + " has the count attribute!")

Code

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

for element in can_we_count_it:
  if hasattr(element, 'count'):
    print(f"{element} has the count attribute!")

The instructions don’t want you to print the actual elements that have the attribute. They want you to print the type of the element.

# Your output:
"sassafras has the count attribute!"
"['a', 'c', 's', 'd', 's'] has the count attribute!"

# Expected output:
"<class 'str'> has the count attribute!"
"<class 'list'> has the count attribute!"
Click for Spoiler Code
print(f"{type(element)} has the count attribute!")
2 Likes

Thank you! That works.

1 Like