How many times a digit appears

Write code · Medium · §7.4, §8.2 Giving back

Complete the function count_digit(n, d), which returns how many times the digit d appears in the positive integer n. Solve it with while, % 10, and // 10. 40100 has three 0s, and 2026 has two 2s. The challenge test expects count_digit(0, 0) to be 1.

Starter code

def count_digit(n, d):
    count = 0
    # Write your loop here
    return count

print(count_digit(40100, 0))
print(count_digit(2026, 2))

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.