 zick - 3 years ago 188
Python Question

# how to write a program

I'm trying to write a program that would output of an item which is worth 10000 over a period of 15 years using this formula:

I have tried using a method like this:

``````a = 10000000
y = 120
n = 15
d = (n / y * 10000000)
c = (a - d)
count = 1

if count < 16:
print(" ")

while count < 16:
print("End of year", count, "Depression""=", (d), "Current Value""=", (c))
count +=1
`````` Hugh Bothwell
``````def main():
print("Sum-of-the-Years'-Digits Method")

years = 15
sum_of_years = years * (years + 1) // 2    # = sum(1..years)
initial_value = 10000000

value = initial_value
for year in range(years):
depr = (years - year) / sum_of_years * initial_value
value -= depr
print("End of year {}: depreciation = {:0.2f}, new value = {:0.2f}".format(year + 1, depr, value))

if __name__=="__main__":
main()
``````

which gives

``````Sum-of-the-Years'-Digits Method
End of year 1: depreciation = 1250000.00, new value = 8750000.00
End of year 2: depreciation = 1166666.67, new value = 7583333.33
End of year 3: depreciation = 1083333.33, new value = 6500000.00
End of year 4: depreciation = 1000000.00, new value = 5500000.00
End of year 5: depreciation = 916666.67, new value = 4583333.33
End of year 6: depreciation = 833333.33, new value = 3750000.00
End of year 7: depreciation = 750000.00, new value = 3000000.00
End of year 8: depreciation = 666666.67, new value = 2333333.33
End of year 9: depreciation = 583333.33, new value = 1750000.00
End of year 10: depreciation = 500000.00, new value = 1250000.00
End of year 11: depreciation = 416666.67, new value = 833333.33
End of year 12: depreciation = 333333.33, new value = 500000.00
End of year 13: depreciation = 250000.00, new value = 250000.00
End of year 14: depreciation = 166666.67, new value = 83333.33
End of year 15: depreciation = 83333.33, new value = 0.00
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download