harderthanithought
harderthanithought

Reputation: 13

Trouble with the while loop

I am trying to write a simple code that when given , 2 numbers, one integer and the other the divisor, how to return the number of times the given integer can be divided by the divisor until the quotient is less than 1. I got it to divide the two numbers but I cant figure out how to get it to tell me the amount of numbers that were divided.

Example:

123 / 2 

Should return a value of

7

My code:

def another_one(integer, divisor):
    while integer > 0:
        integer //= divisor
        print(integer)
    return integer

Thanks

Upvotes: 1

Views: 38

Answers (2)

Norden
Norden

Reputation: 31

I'm not sure I understand correctly. Perhaps you need something like this

def another_one(integer, divisor):
    counter = 0
    while integer > 0:
        counter += 1
        integer //= divisor
        print(integer)
    return counter

print(another_one(123, 2))

Upvotes: 0

Craig
Craig

Reputation: 4855

You forgot to keep track of the number of times the loop iterates:

def another_one(integer, divisor):
    count = 0
    while integer > 0:
        integer //= divisor
        print(integer)
        count += 1
    return count

Upvotes: 3

Related Questions