Coursera issue for me

Question:

Please help me, i doing an online course and i was asked this question

The fractional_part function divides the numerator by the denominator, and returns just the fractional part (a number between 0 and 1). Complete the body of the function so that it returns the right number.
Note: Since division by 0 produces an error, if the denominator is 0, the function should return 0 instead of attempting the division.

here is my code:

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to 
# keep just the fractional part of the quotient
   if denominator == 0 or numerator == 0:
      print("0")
   else:
        fraction = numerator / denominator
        while fraction > 1:
           fraction1 = fraction - 1
   return fraction1

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0

This is my output:

UnboundLocalError: local variable ‘fraction1’ referenced before assignment

Asked By: Precious Eyoh

||

Answers:

You have to define fraction1 in your code.

You are only creating fraction1 variable inside the while loop and that while loop only runs when fraction > 1.

What if fraction <= 1? You don’t have any variable called fraction1 created anywhere in your code.

And at the end your function returns fraction1 which isn’t defined.

Also you must decrease fraction by 1 inside the loop else the loop goes on indefinitely.

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to 
# keep just the fractional part of the quotient
   if denominator == 0 or numerator == 0:
       return 0
   else:
       fraction = numerator / denominator
       fraction1 = 0
       while fraction > 1:
           fraction1 = fraction - 1
           fraction -= 1
       return fraction1
        
Answered By: Ram

If you change fraction1 to fraction you will solve the problem that fraction1 is not defined. Then you need to add fraction = 0 after the first if to solve if denominator or numerator is 0

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to 
# keep just the fractional part of the quotient
   if denominator == 0 or numerator == 0:
      fraction = 0 #to solve if denominator or numerator is 0
      print("0")
   else:
        fraction = numerator / denominator
        while fraction > 1:
           fraction = fraction - 1
   return fraction
Answered By: Sefan

Here following a mathematical approach: for positive values only (sign check should be done in a pre-processing phase)

def fractional_part(numerator, denominator):
    # Operate with numerator and denominator to
    # keep just the fractional part of the quotient
    if denominator == 0:
        return 0 #raise Exception('Not possible')
    elif numerator == 0:
        return 0
    # check if result of division is already smaller than 1
    if numerator < denominator:
       return numerator / denominator

    remainder  = numerator % denominator
    return remainder / denominator

print(fractional_part(23, 5))
print(fractional_part(5, 4))

Output

0.6
0.25

A different implementation compatible with negative numbers (without pre-processing phase)

def fractional_part(numerator, denominator):
    if denominator == 0:
        return 0 #raise Exception('Not possible')
    elif numerator == 0:
        return 0
        
    sign_counter = 0
    if numerator < 0:
        sign_counter += 1
    if denominator < 0:
        sign_counter += 1
    numerator, denominator = abs(numerator), abs(denominator)
    
    # check if result of division is already smaller than 1
    if numerator < denominator:
       return (-1)**sign_counter * numerator / denominator

    remainder  = numerator % denominator
    return (-1)**sign_counter * remainder / denominator

More compact way

def fractional_part(numerator, denominator):
    if denominator == 0:
        return 0 #raise Exception('Not possible')
    decimal = numerator / denominator
    return decimal - int(decimal)

Answered By: cards

to solve this you will need to remove the Floor division so we use this Python Arithmetic Operator //

def fractional_part(numerator, denominator):
      if denominator == 0 or numerator == 0:
         return 0

      else:
          fraction = ((numerator / denominator)-(numerator // denominator))
          return fraction
    

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0

Output

0.0
0.25
0.6666666666666667
0.5
0
0
Answered By: Proton
def fractional_part(numerator, denominator):
    if numerator==0 or denominator==0:
        return 0
    else:
        result=numerator/denominator
        while result>=1:
            result-=1
        return result   

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0
Answered By: Neeraj Tripathi
def fractional_part(numerator, denominator):
    if numerator == denominator or denominator ==0 or numerator == 0:
        return 0
    else:
        result=numerator/denominator
        if result > 1 : 
           result1=result-int(result)
           return result1   
        return result              
    print(fractional_part(5, 5)) # Should be 0
    print(fractional_part(5, 4)) # Should be 0.25
    print(fractional_part(5, 3)) # Should be 0.66...
    print(fractional_part(5, 2)) # Should be 0.5
    print(fractional_part(5, 0)) # Should be 0
    print(fractional_part(0, 5)) # Should be 0
Answered By: Darshan Pawar
def fractional_part(numerator, denominator):
    if denominator == 0 or numerator == 0:
       return 0
    else:
      result = numerator / denominator
      if result >= 2:
            result1 = result - int(2)
            return result1
      elif result >= 1:
            result2 = result - int(1)
            return result2

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0
Answered By: Vignesh M

Operate with numerator and denominator to
keep just the fractional part of the quotient
use division and modulo operator

Answered By: fayaz
def fractional_part(numerator, denominator):
    if denominator!=0:
         fraction=numerator/denominator
         integer= numerator//denominator
         result= fraction-integer
         return result
    return 0
Answered By: Prasant Pant
def fractional_part(numerator, denominator):

    if numerator==0 or denominator==0 : #check is any of the number is zero
        return 0   #directly return zero as directed in the question
    elif numerator%denominator==0: #Check if denominator divides numerator completely
        return 0  #return zero, as there is no digit after decimal to be returned.
    else:
        fractional= numerator/denominator #store the value of quotient in variable fractional
        fractional-=int(fractional) #subtract the number before decimal from quotient by converting it to integer
        return fractional  #return the value of fraction to the function

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0
Answered By: Abhishek Yadav

Instead of using loops we can use // operator

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to

keep just the fractional part of the quotient

if(numerator==0 or denominator==0):
    return 0
else:
    return ((numerator/denominator)-numerator//denominator)
Answered By: 29_MAYANK MISHRA

def fractional_part(numerator, denominator):
if numerator == 0 or denominator == 0:
fraction = 0
return 0
elif numerator % denominator == 0:
return 0
else:
fraction = numerator / denominator
while fraction > 1:
fraction = fraction – 1
return fraction

Answered By: Elias Qaid
def fractional_part(numerator, denominator):
          if denominator == 0 or numerator == 0:
             return 0
          else:
              return ((numerator / denominator)-(numerator // denominator))
    print(fractional_part(5, 5)) # Should be 0
    print(fractional_part(5, 4)) # Should be 0.25
    print(fractional_part(5, 3)) # Should be 0.66...
    print(fractional_part(5, 2)) # Should be 0.5
    print(fractional_part(5, 0)) # Should be 0
    print(fractional_part(0, 5)) # Should be 0


Output:
0.0
0.25
0.6666666666666667
0.5
0
0
Answered By: Laiba Akram
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.