How to Count event in python
most of the time we want our programs to count how many times something happens. For instance, a game may need to keep track of how many turns a player has used, or a math program may
want to count how many numbers have a special property. The solution to counting is to use a variable to keep the counts.
To count in almost all languages you can use the following steps:
stepI:
set a variable like "count" to equal 0
count=0
stepII:
write a for loop given the range
for i in range(your range)
stepIII:
write the code for the event you want to count inside the for loop
stepIV:
increase the count variable by 1 i.e
count=count+1
stepV:
you can print your counter now but not in the for loop
print(count)
Let's try to apply the steps:
Example 1
This program gets 5 numbers from the user and counts how many of those numbers even.
count = 0
for i in range(5):
num = eval(input('Enter a number: '))
if num%2==0:#to check if divisible by 2
count=count+1
print('There are', count, 'numbers even.')
Result:
Enter a number: 4
Enter a number: 9
Enter a number: 78
Enter a number: 789
Enter a number: 98
There are 3 numbers even.
The best way to get how it works is to think of the count variable as if we are keeping a tally on a piece of paper. so every time we get a number divisible by 2, we add 1 to our tally. In the program, this is accomplished by the linecount=count+1. The first line of the program, count=0, is very important. for Without it, it gives error.
Example 2
This modification of the previous example counts how many number of the user are divisible by 5 and also how many are greater than 22.
in this question we need to count two things. so To count two things we use two count variables.
count1 = 0
count2 = 0
for i in range(5):
num = eval(input('Enter a number: '))
if num>22:#for number greater than 22
count1=count1+1 # greater than counter
if num%5==0: # divisible by 5 event
count2=count2+1 #divisible by 5 counter
print('There are', count1, 'numbers greater than 22.')
print('There are', count2, 'numbers divisible by 5')
Result:
Enter a number: 4
Enter a number: 9
Enter a number: 78
Enter a number: 789
Enter a number: 98
There are 3 numbers greater than 22.
There are 0 numbers divisible by 5
Example 3
Now let's try to solve slightly tough example. This program counts how many perfect square
between 1 to 1000 end in with digit 1.
count = 0
for i in range(1,32):
if (i**2)%10==1:
count = count + 1
print(count)
Result
7
Note:
First, the range here is 32 because that is the smallest number that its square could give us number more than 1000 since we need to square the "i"
32^2=1024
The looping variable i takes on those
values, so the squares from 1^2 to 32^2 are represented by i**2. Next, to check if a number ends in 1, a nice mathematical trick is to check if its remainder is 1 when divided by 10. The
modulo operator, %
, is used to get the remainder.
and our counter will start counting. did you understand? if yes please like and comment next post we are going to learn simple multplication game for kids to learn more about counting.
I love coding, you enjoy coding,Yeah! coding is fun!
Top comments (0)