如何在循环中对总计算值求和(Python 2.7)

时间:2015-07-23 15:08:00

标签: python loops

我想使用下面显示的代码来计算该期间的年度总租金。

非常感谢任何帮助。

rent_rate = input('please enter the price per square foot:')   # price sq/ft
lease_rate = input('please enter the rate of the lease:')           # interest rate
lease_term = input('please enter the tenor (in years) of the lease:')  
size_of_space = input('please enter the size(in square feet) of the space:')  

print "Year %25s" % "Rent Rate%25s"  % "Rate%25s" % "Square Feet%25s" % "Yearly Rent%25s" % "Monthly Rent"


for year in range( 0, lease_term ):
   rent_rate = principal * ( 1.0 + rate ) ** year
   yearly_rent = rent_rate * size_of_space
   monthly_rent = yearly_rent/12
   print "%4d%21.2f%21.2f%21.2f%21.2f%21.2f" % ( year + 1, rent_rate, rate, size_of_space, yearly_rent, monthly_rent)

1 个答案:

答案 0 :(得分:0)

我认为你需要做一些事情!您应该指定' principal'以及' rent_rate'因为它们是凭空而来的!

另外,如果用户输入的是数字,你应该做' int'或者'漂浮'那些投入!这里:

rent_rate = float(input('please enter the price per square foot:'))   
lease_rate = float(input('please enter the rate of the lease:'))       
lease_term = int(input('please enter the tenor (in years) of the lease:'))  
size_of_space = float(input('please enter the size(in square feet) of the space:'))  

print "Year %25s" % "Rent Rate%25s"  % "Rate%25s" % "Square Feet%25s" %  "Yearly Rent%25s" % "Monthly Rent"


for year in range(0, lease_term):
   rent_rate = principal * ( 1.0 + rate ) ** year
   yearly_rent = rent_rate * size_of_space
   monthly_rent = yearly_rent/12
   print "%4d%21.2f%21.2f%21.2f%21.2f%21.2f" % ( year + 1, rent_rate, rate,     size_of_space, yearly_rent, monthly_rent)

就像风格一样 - 在线评论应该谨慎进行。查看python Pep 8风格指南!