Design a program to Count numbers that don’t contain 3 in Python

Hello Learners, today we are going to design a program that can count numbers that don’t contain 3 in it using Python. From this Python tutorial, you can learn how to count numbers that don’t contain a specific number.

Objective:

Suppose you have a list of N numbers and you have to find all the numbers that do not contain three in it. Your program will count all such numbers.

For example, numbers like 6259, 208, 95 etc do not contain three in it so it will be count.

and numbers like 305, 8321, 34 will not be count because it contains 3three in it.

Looks simple, let’s jump into the code directly for the better understanding.

n = int(input())
l = []
for i in range(n):
    l.append(input())
c = 0
for i in range(n):
    if('3' not in str(l[i])):
        c = c + 1
print(c)

OUTPUT:

5
123
234
345
456
567
count of numbers without 3 is: 2
Explenation:

Let’s trace the code line by line to see how it works:

  • Create an empty list to hold the numbers.
  • Take a number n as input for the number of elements you want in your list.
  • Add a for loop up to that number for taking input in your list using append method.
  • create another variable c to hold the count of such numbers that do not contain three.
  • Add another for loop to n i.e. the number of elements in the list.
  • Inside the for loop convert each element of the list into a string using str() method and then check if three is there in the string or not.
  • If three is not found in the string then increment the count variable c by one.
  • At the end of the for loop print the value of count variable c to check how many such numbers are there in the list that do not contain three in it.

Done, easy right! try it on your own.

So, that’s all for now about designing a program that can count numbers that don’t contain 3 in it using Python.

Leave a Reply