My program runs but definitely not how it's meant to. No matter which input I put it, it always says "You've earned: $0.00" and I'm tried moving "months_invested = years_investment / 12" to multiple places and even adding or taking away bits.
The investment calculator is meant to compound this stuff monthly but I cannot seem to get it right. One problem turns to two and it's like the hydra to me, where you fix one thing and it multiplies itself.
"""
InvestmentCalculator.py helps generate how much interest one earns after a certain period of time
"""
def main():
total_money = 0
months_invested = 0
years_investment = 0
investment = float(input("How much would you like to invest? "))
years_investment = float(input("How many years would you like to invest? "))
interest_rate = float(input("What is the interest rate? "))
total_money = float()
months_invested = float()
while months_invested > 0:
months_invested = (years_investment / 12) - 1
total_money = investment + total_money
print("You have earned ${:,.2f}".format(total_money))
else: print("You've earned a total of ${:,.2f}".format(total_money))
main()
I have a feeling this is what you are trying to achieve. There were some adjustments to be made which I'll explain below the code:
def main():
total_money = float(input("How much would you like to invest? "))
years_investment = float(input("How many years would you like to invest? "))
interest_rate = float(input("What is the monthly interest rate in %? "))
months_invested = years_investment * 12
while months_invested > 0:
interests = interest_rate / 100 * total_money
total_money += interests
print("You have earned ${:,.2f}".format(total_money))
months_invested += -1
main()
Output:
You have earned $101.00
You have earned $102.01
You have earned $103.03
You have earned $104.06
You have earned $105.10
You have earned $106.15
You have earned $107.21
You have earned $108.29
You have earned $109.37
You have earned $110.46
You have earned $111.57
You have earned $112.68
As G.Anderson stated, there's no need to initialize variables with value 0 (or any value). Simply define it as the input.
The months investment formula was wrong and troubled the loop. Also, if that was fixed, the loop would run infinitely since there was no limit or condition that would end it (that's what the months_invested += -1
is for. To decrease the value of the variable each time the loop passes so it stops sometime (that is, when there are no more investments months).
Finally remember your function is printing a result and not returning anything so be careful when using it in advance.
If you needed compound interest (monthly) then you should use the formula for compound interest. That is (((1+i) ** months) -1) * $
. In this case I'm using total_money += interests
to sum the interests generated in the period to the total amount of money, which will be reinvested in the following period (hence, compound interest).
There is no real need to use a while loop, other approaches might be more efficient, using lists or arrays for instance. However I respected the original structure of your code for my answer.
months_invested
is set by the line months_invested = float()
, where it is initialized to zero. This means that the while loop never runs. You want months_invested = years_investment * 12
.
It's worth noting that this still won't do what you want, but I don't want to provide you with an entire answer: struggling with problems is an important part of learning.
You've got a couple things going here.
float()
before hitting the while loop. years_investment = 0
is not in the main()
function. (this is probably just a copy/paste issue when posting the question.) months_investment
value.I did some refactoring to get you where I think you want to be.
def main():
investment = float(input("How much would you like to invest? "))
months_investment = float(int(input("How many years would you like to invest? ")) * 12)
interest_rate = float(input("What is the interest rate? "))
total_money = 0
while months_investment > 0:
total_money += investment * interest_rate
print("You have earned ${:,.2f}".format(total_money))
months_investment -= 1
else: print("You've earned a total of ${:,.2f}".format(total_money))
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.