-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction problem.txt
More file actions
23 lines (19 loc) · 995 Bytes
/
fraction problem.txt
File metadata and controls
23 lines (19 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
def fractional_part(numerator, denominator):
# Operate with numerator and denominator to
# keep just the fractional part of the quotient
if numerator==0 :
return 0
elif numerator==denominator :
return 0
elif denominator!=0 :
hold= numerator%denominator
fraction=hold/denominator
return fraction
return 0
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