ORM ORM - 12 days ago 7
Python Question

Incorporate a for loop into integer division

I want to divide one number by another number using integer division until my result is zero and keep track of how many divisions I do. However I keep getting an 'int' object not iterable error whenever I try to incorporate a for loop. My code without a for loop is:

x = 4
y = 2
numofdiv = 0
if x // y != 0:
x // y
numofdiv += 1


Any suggestions of how I can incorporate a for loop or any other approaches for this problem?

Answer

There are many ways but here are 2 simple ones:

x=4
y=2
numdiv = 0
while True:
    if x//y > 0:
        x= x//y
        numdiv +=1
    else:
        break
print (numdiv)

or

x=4
y=2
numdiv = 0
while x//y > 0:
        x= x//y
        numdiv +=1
print (numdiv)