我如何应用循环来消除这个复杂的利率计算器的线条?
How can I apply loops to shave off lines from this convoluted interest rate calculator?
- 如何从这段代码中删除行?我觉得我可以使用循环来简化这个过程。
- 我刚开始学习 python 以使我的数学学习更有趣。我对编码几乎一无所知,所以举个例子会有很大帮助。
- 该代码必须包含一个选项以投入初始资金。
谢谢!
capital = float(input("Enter initial capital: "))
interest_rate = float(input("Enter interest rate: "))
annual_saving = float(input("Enter annual savings: "))
year = float(input("Enter number of years of saving: "))
# Y stands for Year
Y0 = capital + annual_saving
Y1 = Y0 * (1 + interest_rate)
Y2 = (Y1 + annual_saving) * (1 + interest_rate)
Y3 = (Y2 + annual_saving) * (1 + interest_rate)
Y4 = (Y3 + annual_saving) * (1 + interest_rate)
Y5 = (Y4 + annual_saving) * (1 + interest_rate)
Y6 = (Y5 + annual_saving) * (1 + interest_rate)
Y7 = (Y6 + annual_saving) * (1 + interest_rate)
Y8 = (Y7 + annual_saving) * (1 + interest_rate)
Y9 = (Y8 + annual_saving) * (1 + interest_rate)
Y10 = (Y9 + annual_saving) * (1 + interest_rate)
Y11 = (Y10 + annual_saving) * (1 + interest_rate)
if year == 1:
print(Y1)
if year == 2:
print(Y2)
if year == 3:
print(Y3)
if year == 4:
print(Y4)
if year == 5:
print(Y5)
if year == 6:
print(Y6)
if year == 7:
print(Y7)
if year == 8:
print(Y8)
if year == 9:
print(Y9)
if year == 10:
print(Y10)
应计利息的公式是 K_interrest = K_start * (1 + (p/100))**n
,其中 p
作为您的利率,n
作为年限。这样我们就可以将您的代码放在一个方便的函数中。你的初始资金和年储蓄乘以利率得到你的新资本。这种情况发生 n
次。
def calculate_savings(k, p, a, n):
for i in range(n):
k = (k+a) * (1 + (p/100))
print(f"{k:.2f}", "$") # will print your result with 2 decimal digits
capital = float(input("Enter initial capital: "))
interest_rate = float(input("Enter interest rate [%]: ")) # input in percent
annual_saving = float(input("Enter annual savings: "))
years_duration = int(input("Enter number of years of saving: ")) # years must be int here
calculate_savings(capital, interest_rate, annual_saving, years_duration)
另一种可能是使用递归函数(调用自身的函数):
def calculate_savings(k, p, a, n):
if n <= 0:
print(f"{k:.2f}", "$")
else:
k = (k+a) * (1 + (p/100))
calculate_savings(k, p, a, n-1) # calculates k with one year less
通常认为保持代码干燥(不要重复自己)是一种很好的做法。此外,当您的脚本变得更加丰富时,您应该考虑捕获错误的输入。
您可以创建一个函数来进行所有计算并 return 得到正确的结果。函数从输入中获取 4 个变量。
def calculations(capital, interest_rate, annual_saving, year):
x = (capital + annual_saving) * (1 + interest_rate)
for i in range(1, year, 1):
x = (x + annual_saving) * (1 + interest_rate)
return x
capital = float(input("Enter initial capital: "))
interest_rate = float(input("Enter interest rate: "))
annual_saving = float(input("Enter annual savings: "))
year = float(input("Enter number of years of saving: "))
results = calculations(capital, interest_rate, annual_saving, year)
print(results)
- 如何从这段代码中删除行?我觉得我可以使用循环来简化这个过程。
- 我刚开始学习 python 以使我的数学学习更有趣。我对编码几乎一无所知,所以举个例子会有很大帮助。
- 该代码必须包含一个选项以投入初始资金。
谢谢!
capital = float(input("Enter initial capital: ")) interest_rate = float(input("Enter interest rate: ")) annual_saving = float(input("Enter annual savings: ")) year = float(input("Enter number of years of saving: ")) # Y stands for Year Y0 = capital + annual_saving Y1 = Y0 * (1 + interest_rate) Y2 = (Y1 + annual_saving) * (1 + interest_rate) Y3 = (Y2 + annual_saving) * (1 + interest_rate) Y4 = (Y3 + annual_saving) * (1 + interest_rate) Y5 = (Y4 + annual_saving) * (1 + interest_rate) Y6 = (Y5 + annual_saving) * (1 + interest_rate) Y7 = (Y6 + annual_saving) * (1 + interest_rate) Y8 = (Y7 + annual_saving) * (1 + interest_rate) Y9 = (Y8 + annual_saving) * (1 + interest_rate) Y10 = (Y9 + annual_saving) * (1 + interest_rate) Y11 = (Y10 + annual_saving) * (1 + interest_rate) if year == 1: print(Y1) if year == 2: print(Y2) if year == 3: print(Y3) if year == 4: print(Y4) if year == 5: print(Y5) if year == 6: print(Y6) if year == 7: print(Y7) if year == 8: print(Y8) if year == 9: print(Y9) if year == 10: print(Y10)
应计利息的公式是 K_interrest = K_start * (1 + (p/100))**n
,其中 p
作为您的利率,n
作为年限。这样我们就可以将您的代码放在一个方便的函数中。你的初始资金和年储蓄乘以利率得到你的新资本。这种情况发生 n
次。
def calculate_savings(k, p, a, n):
for i in range(n):
k = (k+a) * (1 + (p/100))
print(f"{k:.2f}", "$") # will print your result with 2 decimal digits
capital = float(input("Enter initial capital: "))
interest_rate = float(input("Enter interest rate [%]: ")) # input in percent
annual_saving = float(input("Enter annual savings: "))
years_duration = int(input("Enter number of years of saving: ")) # years must be int here
calculate_savings(capital, interest_rate, annual_saving, years_duration)
另一种可能是使用递归函数(调用自身的函数):
def calculate_savings(k, p, a, n):
if n <= 0:
print(f"{k:.2f}", "$")
else:
k = (k+a) * (1 + (p/100))
calculate_savings(k, p, a, n-1) # calculates k with one year less
通常认为保持代码干燥(不要重复自己)是一种很好的做法。此外,当您的脚本变得更加丰富时,您应该考虑捕获错误的输入。
您可以创建一个函数来进行所有计算并 return 得到正确的结果。函数从输入中获取 4 个变量。
def calculations(capital, interest_rate, annual_saving, year):
x = (capital + annual_saving) * (1 + interest_rate)
for i in range(1, year, 1):
x = (x + annual_saving) * (1 + interest_rate)
return x
capital = float(input("Enter initial capital: "))
interest_rate = float(input("Enter interest rate: "))
annual_saving = float(input("Enter annual savings: "))
year = float(input("Enter number of years of saving: "))
results = calculations(capital, interest_rate, annual_saving, year)
print(results)