-1

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?

ORM
  • 25
  • 1
  • 4
  • 6
    using a `while` loop would be smarter – depperm Nov 28 '16 at 16:17
  • 2
    That code will never stop, since you never modify x or y. But if you need help with other code you have written, you should post that. – Daniel Roseman Nov 28 '16 at 16:18
  • 1
    Yeah, sorry, misread it as while. – Daniel Roseman Nov 28 '16 at 16:23
  • should `x` be updated to `x/y`? Can you show your for loop code that you are getting an error on? – depperm Nov 28 '16 at 16:27
  • 1
    Just what do you mean by "divide one number by another number using integer division until my result is zero"? Repeating the operation on the same numbers gives the same result. Do you mean replace one of the numbers by the remainder, as in the Euclidean algorithm? Or use the quotient, or something else? – Rory Daulton Nov 28 '16 at 16:31

4 Answers4

0

Here is one way to do it:

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

print(numofdiv)
Steve
  • 1,250
  • 11
  • 25
0

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)
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
0

Using a while loop checking if condition is met and it will stop if the condition is not met:

x = 4
y = 2
numOfDiv = 0
while x > 0:
    x = x // y
    numOfDiv += 1
print("number of divison:{}".format(numOfDiv))

Creating a generator function that gives us a value and it will stop generating if the condition isn't met, using the same while loop. We can then iterate from the generator until all the values is exhausted using a for loop.

x = 4
y = 2

def int_div(x,y):
  while x > 0:
    x,y = x // y, y 
    yield x

for numOfDiv, value in enumerate(int_div(x,y),1):
  pass
print("number of divison:{}".format(numOfDiv))

Using the same generator in a list comprehension to find all the values we want, and then printing the size of the list since that's how many division it made.

x = 4
y = 2

def int_div(x,y):
  while x > 0:
    x,y = x // y, y 
    yield x

a = [i for i in int_div(x,y)]
print("number of divison:{}".format(len(a)))
MooingRawr
  • 4,901
  • 3
  • 24
  • 31
0

As pointed out, a while loop is probably more appropriate. A for loop version might look like this:

x=4
y=2
divisions = 0
max_divisions = 1000

for i in range(max_division):
    if x//y > 0:
        x = x//y
        divisions +=1
    else:
        break
print(divisions)
Martin Bonde
  • 536
  • 3
  • 11