初学者尝试询问输入并计算未来值

时间:2019-12-25 21:11:07

标签: python

Investment=float(input("How much do you want to invest?"))
Rate =float(input("what yield are you receiving?"))
Periods = int(input("how many periods is your investment?"))
periods_invest=(Rate/100**Periods)
Return_invest = (Investment)*(1 + Rate/100)*(periods_invest)
print(f"You receive ${Return_invest:.2f}")

背景是我没有得到适当的回报率。

1 个答案:

答案 0 :(得分:0)

您要计算compound interest吗?您需要使用正确的运算符,并注意运算符的优先级:在Python中,**是求幂运算符。它的operator precedence比乘法运算*高。这意味着a * b ** ca * (b ** c)而不是(a * b) ** c

investment = float(input("How much do you want to invest? "))
rate = float(input("What yield are you receiving? "))
periods = int(input("How many periods is your investment? "))
return_invest = investment * (1 + rate / 100) ** periods
print(f"You receive ${return_invest:.2f}")

以下是该代码的示例运行:

How much do you want to invest? 1000
What yield are you receiving? 5
How many periods is your investment? 50
You receive $11467.40

此外,我遵循snake case命名约定使用了变量名。有关更多信息,请参见PEP 8