【发布时间】:2017-03-23 18:55:09
【问题描述】:
我正在用更新的代码重新发布这个问题。
当我运行此代码来计算多年来获得的单利时(以漂亮的表格形式),我的表格列从 5 开始。此外,总数是从 5 开始计算的。只是为了澄清,当询问 totYear 时,我使用 5 作为用户输入的示例。
#Declare the necessary variables.
princ = 0
interest = 0.0
totYear = 0
year = 1
#Get the amont of principal invested.
print("Enter the principal amount.")
princ = int(input())
#Get the interest rate being applied.
print("Enter the interest rate.")
interest = float(input())
#Get the total amount of years principal is invested.
print ("Enter the total number of years you're investing this amonut.")
totYear = int(input())
print("Year Interest")
for year in range(totYear):
total=totYear*interest*princ
print (totYear," $",total)
totYear+=1
if total<100:
print("That is not too much interest!")
else:
print("This interest really adds up!")
这是输出画面:
Enter the principal amount.
10
Enter the interest rate.
5
Enter the total number of years you're investing this amonut.
5
Year Interest
5 $ 250.0
6 $ 300.0
7 $ 350.0
8 $ 400.0
9 $ 450.0
This interest really adds up!
感谢您的帮助!
【问题讨论】:
-
您想打印
year,因为这是将通过该范围的变量。我认为您不需要增加totYear -
尝试打印
year而不是totYear
标签: python python-3.x for-loop logical-operators