I'm trying to write code to count the number of times a whole number can be divided by 2 before reaching 1.

When I run my code it prompts me to enter a number as you'll see in my code below, but once I do so, nothing happens, just a blank line appears. Does anyone know what I'm doing wrong? It's probably something so simple and insignificant but I'm new with Python. Thanks!

def main():
    n = eval(input("Enter a number: "))
    count = 0
    while n > 0:
        if n // 2 > 1:
            count = count + 1
    print(count)
   
main()

That's because there is an infinite loop in the program. Because "n" does not change, the while loop never exits. We have no way of knowing how you want to change the number if it is divisible by 2, or how you want to change it if it is not. When testing a while loop it is a good idea to limit the number of iterations. This is an illustration of how to limit the loop, not how to change it to exit properly.

limit = 0
    while (n > 0) and (limit < 1000):
        limit += 1

I would go with something like this, and why is there a main() function for no reason? Note that only the powers of 2 can be divided down to 1.

## Assumes Python 3.X
#
def get_input():
    while True:
        n = input("Enter a number: ")
        try:
            return int(n)
        except:
            print("Enter a number only")

orig_number = get_input()
count = 0
number = orig_number
next = True
while next:
    number, remain = divmod(number, 2)
    if remain != 0:
        print("You can divide %d by two %d times" % (orig_number, count))
        next = False    
    count = count + 1

The mathematical result is the logarithm of base 2 of n. It seems to work also for python:

>>> from math import log
>>> n = 444444444444444448888888888888888888888888883333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333336666666666666666666666666666666666666666666666666666666666666666666666666666666666666666677777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777774
>>> k = int(log(n, 2))
>>> n >> k
1L
>>> k
1374

One could expect that this technique fails if n is too large because math.log() normally manipulates float values, but even if it fails, the returned value could be used as a good first approximation which could be refined. Also, even "by hand", an algorithm in O(log(log(n)) is possible.

Simplistic but maybe fast enough way (somehow tested in my mobile):

def bitlength(n):
     for b in (shift for shift in itertools.count() if n>>shift < 1):
         return b-1
     return 0

But actually newest Python versions have it builtin as bitlength attribute of integers. You could use next function instead of the loop, but this phones Python does not sipport it (version 2.5).

count = 0
num = 0

print ('This program calculates how many times 2 can be factored into the number entered')
num = int(input("Enter number : "))

while num > 1:
    num = num / 2
    count = count + 1
print count

Here's the simplest program that will do what you wish you just have to turn it into a callable function. That's what I'm assuming you want to do.

@Rick345, your program works but the print is misleading

print ('This program calculates how many times 2 can be factored into the number entered')

It's a different number which could be obtained with

count = 0
while True:
    num, remainder = divmod(num, 2)
    if remainder:
        break
    else:
        count += 1

I've only been using Python for a day and it took me a little while to grasp your code but, I understand it now. However, I beginning to wonder if either of us understood what Python wanted to know.

I assumed he wanted code that would count the number of times 2 could be divided into a number before the subsequent quotient would be 1 or less.
user inputs 10
num=10
10/2=5
5/2=2.5
2.5/2=1.25
1.25/2=.625
so 10 can be divided by 2 three times before the quotient is <1
However, he may have wanted code that "figures out" how many times 2 can be divided into a number without a remainder.

None the less your code taught me a couple of things one the use of the divmod() function and that count +=1 incumbents count by one on each pass. I used to write code in C and C++ years ago and haven't written a program in years but, thought I'd give Python a whirl as a hobby... I've been reading docs online but, I learn best by looking at code and try and figure out what's going on.

Thanks for the lessons.

@Rick345, your program works but the print is misleading

It's a different number which could be obtained with

count = 0
while True:
    num, remainder = divmod(num, 2)
    if remainder:
        break
    else:
        count += 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.