Hello, Im trying to use “for loop” function to calculate present value but I couldn’t get the correct answer.
The situation is that assuming I receive a bonus at the end of every year. The bonus starts at $1000 and increases by $10 every five years. The annual interest rate is 5%.
For example, the present value of year 6 is $5083 but I got $6501. How can I solve this problem. Thanks

`pv = 0
year = eval(input('Enter the number of year: '))

for i in range(1,(year)+1):
if year <= 5:
pv += (1000)/((1.05)**i)
else:
x = year//5
pv += (1000)+(10*x)/((1.05)**i)
y = round(pv)
print(f’The present value is ${y}.')`

Don’t use eval. If you need to convert the number of years from a string to an int, use int:

year = eval(input('Enter the number of year: ')) # Slow, dangerous if the input is untrusted.
year = int(input('Enter the number of year: ')) # Fast, always safe!

Start by calculating the value by hand:

End of year 0: pv = 0
End of year 1: pv = 0*1.05 + 1000 = 1000
End of year 2: pv = 1000 * 1.05 + 1000 = 2050
End of year 3: pv = 2050 * 1.05 + 1000 = 3152.5
End of year 4: pv = 3152.5 * 1.05 + 1000 = 4310.125
End of year 5: pv = 4310.125 * 1.05 + 1000 = 5525.63125
# Change of bonus!
End of year 6: py = 5525.63125 * 1.05 + 1010 = 6811.912812500001

So by this calculation, both of the figures you quote ($5083 and $6501) are wrong.

It is possible that I have made a mistake, but if you have checked my calculations and agree with them, then you can take that manual calculation and turn it into a program.