iivri  andre
iivri andre

Reputation: 51

division by repeated subtraction in python - What is wrong with this code?

quur = int(input('ENTER NUMBER : '))
quub = int(input('ENTER NUMBER : '))

def quureC (quur) : 

    while quur > 0 :

        quur - quub

print(" The quotient of 10 is ", (quureC (quur))

Upvotes: 0

Views: 4495

Answers (4)

Rohini Lakshmi
Rohini Lakshmi

Reputation: 1

CORRECTED CODE in 3.7

def divide(a,b,i=1):
    if a>b:
        c=a-b
        return divide(c,b,i+1)
    else:
        return i
print("Quotient is:",divide(n1,n2))

This code works only for positive and non-zero values of a & b.

Upvotes: 0

Copperfield
Copperfield

Reputation: 8510

there are 2 thing wrong with your code: first you don't modify the value of quur and therefore you get stuck in a infinite loop, and second the algorithm is wrong, the correct one is subtracting the denominator from the numerator while this is greater than the denominator, whatever remains is the, well, remainder and how many times you do it is the result you are searching for

def division(numerator,denominator):
    if denominator == 0:
        raise ZeroDivisionError
    fraction  = 0
    remainder = numerator
    while remainder >= denominator:
          remainder = remainder - denominator
          fraction  = fraction + 1
    return (fraction, remainder)   

at the end you get number such that numerator == denominator*fraction + remainder were 0 <= remainder < denominator and fraction >= 0

python already have this implemented as // operator, to get the remainder use the modulo operator % and to get both at the same time use divmod. The function above is equivalent to the build-in divmod of python for the most part, but the actual implementation may be different

other thing with your function is that you treat quub as a global variable, while that is not bad per se, it limit the flexibility of your function.


or is you don't like the remainder variable

def division(numerator,denominator):
    if denominator == 0:
        raise ZeroDivisionError
    fraction  = 0
    while numerator >= denominator:
          numerator = numerator - denominator
          fraction  = fraction + 1
    return (fraction, numerator)   

in this case the final numerator is the remainder of the division

Upvotes: 1

Andrew Earls
Andrew Earls

Reputation: 562

So python has this cool operator "% Modulus" that divides left hand operand by right hand operand and returns remainder. I think this is what you are looking for.

quur = 10
quub = 3
print(quur % quub)

>>> 1

However, your code is working but it is getting stuck in an infinite loop because you are not reassigning quur.

Try this:

while quur > 0 :

    quur = quur - quub

I think this works too:

while quur > 0 :

    quur -= quub

So your code would look something like this:

#Gets the inputs from user
quur = int(input('ENTER NUMBER : '))
quub = int(input('ENTER NUMBER : '))



#define a method to do our division by subtraction
def quureC (quur) : 

    #Sets a variable to 0
    count = 0

    # while variable quur is less than or equal to 0 repeat
    while quur >= 0 :

        #quur is equal to quur minus quub
        quur = quur - quub
        #add one to count
        count = count + 1

    #once our while loop condition is met, return the current value of count
    return count

#quotient is assigned whatever is returned from our method quurec
quotient = quureC (quur)

#print the result
print(" the while loop repeated and the quotient is " + quotient)

This should work but I don't think it will return the exact result you are looking for.

Take a look at while loops: http://www.tutorialspoint.com/python/python_while_loop.htm

Hope this helps :)

Upvotes: 2

John Gordon
John Gordon

Reputation: 33335

You aren't modifying the value of quur. You want to do this instead:

while quur > 0:
    quur = quur - quub

Upvotes: 1

Related Questions