Last modified: Dec 19, 2025 By Alexander Williams
Fix Python AttributeError 'int' No 'count'
Python errors can be confusing. The AttributeError is common. It often happens with data types.
This error means you tried to use a method on the wrong object type. The count method does not work on integers.
Let's explore why this error occurs. We will also learn how to fix it for good.
Understanding the 'count' AttributeError
An AttributeError signals a problem. You tried to access an attribute. The object does not have it.
In this case, the object is an integer (int). The attribute is count. Integers are simple numbers.
They do not have a count method. This method is for sequences like strings and lists.
Here is a simple example that causes the error.
# Trying to use .count() on an integer
my_number = 42
result = my_number.count(2) # This line will fail
print(result)
Traceback (most recent call last):
File "script.py", line 3, in
result = my_number.count(2)
AttributeError: 'int' object has no attribute 'count'
The error message is clear. It tells you the object type is 'int'. It says it has no 'count' attribute.
Your job is to find why your variable is an integer. You expected it to be something else.
Why Does This Happen?
This error has common causes. Understanding them is the first step to a fix.
Cause 1: Variable Type Confusion
You might think a variable is a string or list. But your code made it an integer.
This often happens after arithmetic or user input. Check how the variable gets its value.
Cause 2: Method Misapplication
You may know count from strings. You tried to use it on a number by mistake.
Remember, count counts occurrences in a sequence. An integer is not a sequence.
Cause 3: Unintended Integer Conversion
Functions like len() return integers. You might store that result.
Later, you try to call .count() on that stored length value. This causes the error.
How to Diagnose the Problem
Find out what your variable really is. Use the type() function. It reveals the data type.
my_value = 100
print(type(my_value)) # Check the type
Now you know it's an integer. The next step is to trace back. See where it became an integer.
Use print statements before the error line. Check the variable's type and value at each step.
This process is called debugging. It is essential for all programmers.
Common Scenarios and Fixes
Let's look at real code examples. We will see the error and then fix it.
Scenario 1: Counting Digits in a Number
You want to count how many times the digit '2' appears in 1225. You try .count() on the integer.
# Incorrect approach
number = 1225
# This fails because 'number' is an int
count_of_twos = number.count(2)
Fix: Convert the integer to a string first. Then use the string's count method.
# Correct approach
number = 1225
# Convert to string, then count the character '2'
count_of_twos = str(number).count('2')
print(f"Digit '2' appears {count_of_twos} times.")
Digit '2' appears 2 times.
Scenario 2: Function Returns an Integer
You have a function that returns a number. You mistakenly treat the result as a list.
def get_length(data):
return len(data) # Returns an integer
my_list = [1, 2, 3, 2, 1]
length_result = get_length(my_list)
# This fails: length_result is an int, not the original list
count_ones = length_result.count(1)
Fix: Call count on the original sequence, not on the length integer.
def get_length(data):
return len(data)
my_list = [1, 2, 3, 2, 1]
length_result = get_length(my_list)
# Count on the original list
count_ones = my_list.count(1)
print(f"Number 1 appears {count_ones} times in the list.")
print(f"The list length is {length_result}.")
Number 1 appears 2 times in the list.
The list length is 5.
Scenario 3: User Input Mix-up
You ask for user input. You intend to work with a string. But you convert it to an integer too early.
user_data = input("Enter a number: ") # e.g., "2023"
number = int(user_data) # Converts to int immediately
# Now 'number' is an int, you cannot .count()
digit_count = number.count('0')
Fix: Perform string operations before converting to an integer. Or keep a string copy.
user_data = input("Enter a number: ") # e.g., "2023"
# Count on the original string
zero_count = user_data.count('0')
print(f"Digit '0' appears {zero_count} times.")
# Now convert if you need the number for math
number = int(user_data)
General Debugging Strategy
Follow these steps when you see this error.
1. Identify the variable causing the error. Look at the line number in the traceback.
2. Check its type using type(variable_name).
3. Trace its origin. See where it was assigned or changed.
4. Decide your intent. Did you mean to use a string or list instead?
5. Apply the correct method. Use count on the right data type.
Related AttributeErrors
This error is part of a family. You might see similar messages with other types.
For example, a Fix Python AttributeError 'dict' No 'count' occurs with dictionaries.
Or you might get Fix Python AttributeError 'list' No 'count' if you misuse a list.
Even strings can cause confusion, like in Fix AttributeError: 'str' object has no attribute 'count'.
The principle is the same. Check the object type. Use methods that belong to it.
Best Practices to Avoid This Error
Use good variable names. Names should hint at the data type.
For example, use user_input_str for a string. Use item_count_int for an integer.
Add type checking in complex functions. Use isinstance() to confirm types.
Write small pieces of code. Test each part before moving on. This isolates problems.
Read error messages carefully. They point directly to the issue.
Conclusion
The AttributeError 'int' object has no attribute 'count' is a type error. You used a sequence method on a number.
The fix is straightforward. Ensure you call count on the correct data type.
Usually, you need a string or list. Convert your integer if needed. Or use a different variable.
Debug by checking types with type(). Trace your variable's value back through the code.
This error teaches an important lesson. Always know your data types in Python.
Master this, and you will fix this error quickly. You will also write more robust code.