I'm working on a financial calculator. I currently have it so that every time the code loops it adds to a list of results. I intend so that these results will be added to a new list for the inputted amount of times to loop. the modify functions will ensure that the first list has different results each loop due to it randomising the inflation and interest rates.
e.g Input sims is 10 so it will loop 10 times. Each time it loops it will add the list results to list simulations then reset list results. This will result in 10 different lists of lists
currently I am getting the outputs that I need but only the first loop is being added to the second list, so for example it just repeats the first loop 10 times instead of resetting and randomizing each time.
def modify_rate(interest_rate, interest_change):
return interest_rate + interest_change * (1 - random.random() * 2)
def modify_rate(inflation_rate, inflation_change):
return inflation_rate + inflation_change * (1 - random.random() * 2)
def run_simulation():
years, annual_spend, savings_balance, inflation_rate, interest_rate, interest_change, inflation_change, sims = inputs()
simulations = []
i = 0
for x in range(0, sims):
result = []
while i < years:
interest_rate = modify_rate(interest_rate, interest_change)
inflation_rate = modify_rate(inflation_rate, inflation_change)
i = i + 1
totalcost = annual_spend * (1 + inflation_rate) ** 1
annual_spend = totalcost
totalsave = savings_balance - annual_spend
savings_balance = totalsave
x = savings_balance * (1 + interest_rate) ** 1
savings_balance = round(x, 2)
result.append(savings_balance)
if i >=years:
break
elif savings_balance < 0:
break
simulations.append(result)
print(simulations)
I created a mini kind of example of where a calculation will be added x amount of times then the results added to a list, which will be then added to another list x amount of times
x = int(input("enter"))
y = int(input("enter"))
k = int(input("enter"))
a = []
i = 0
h= []
for x in range(0, k):
while i < y:
i = i + 1
z = x + 10 + i
a.append(z)
if i >= y:
break
h.append(a)
print(h)
any guidance would be appreciated :)
xis not available in the while loop.