Division by zero using // operator in python

The floor division // rounds the result down to the nearest whole number (data type = integer) and r as an integer will be 0 when interest is <100.

You can see what happens with top if r equals 0.

Same thing with fraction and the // operator. Use round to round the numbers.

p = int(input("principal: "))
interest = int(input("annual interest rate: "))
years = int(input("# of years: "))

r = interest / 100
n = years * 12
top = r * (1 + r) ** n
bottom = (1+r) ** n - 1
fraction = top / bottom

A = round(fraction * p, 2)
print(A)

Leave a Comment