Skip to content
Advertisement

Trying to understand a code of counting numbers digits in python

Can someone explain to me the code below. I need to understand what line 2 and line 5 mean when we count digits of a number (ex. 100).

JavaScript

Advertisement

Answer

JavaScript

is the variable used to store the number of digits in Number.

JavaScript

this line continues looping until the Number is 0. In other words it loops while Number is a positive integer.

JavaScript

floor division of Number by 10. When you divide a number by 10, you are “removing” its last digit. For example, in the case of 100, it would be

JavaScript

or in the case of 12345,

JavaScript

this effectively reduces the number of digits by one.

JavaScript

increments Count, as we have removed one digit from Number.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement