如何编写程序

时间:2014-12-31 00:45:26

标签: python

我试图编写一个程序,使用这个公式在15年的时间内输出价值10000的项目:

我尝试过使用这样的方法:

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

3 个答案:

答案 0 :(得分:2)

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()

给出了

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

答案 1 :(得分:0)

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()

答案 2 :(得分:-1)

print("Sum-of-the-Years'-Digits Method")<br/>
a = 10000000<br/>
y = 120<br/>
n = 16<br/>
year = 0<br/>
for n in range(15,0,-1):<br/>
    ans = ( (float(n)/y) * 10000000 )<br/>
    a = a - ans<br/>
    year = year + 1<br/>
    print "End of year %d , Depression = %f , Current value = %f"%(year,ans,a)<br/>
print ("Bye,have a nice day!")<br/>