0

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 :)

1
  • Note that the input x is not available in the while loop. Commented May 2, 2018 at 13:22

3 Answers 3

1
x = int(input("enter"))
y = int(input("enter"))
k = int(input("enter"))
h= []
for x in range(0, k):
    a = []
    i = 0
    while i < y:
            i += 1
            z = x + 10 + i
            a.append(z)
    h.append(a)  
print(h)

Every time for loop executes the list ‘a’ is automatically reset and you do not need to specify if condition in while as the condition is already there in while

Sign up to request clarification or add additional context in comments.

3 Comments

as in the original question, the x variable defined in the first line, will not be accessible in the while loop.
Hey, I had a go of changing it around, but the problem is it still just prints the same first list over and over. Is this because the inputs arent resetting after each loop?, I guess the "x" in range" should have been a different letter
@Quickbeam2k1 if we want to access the x variable which is being defined in first line we can use globals()[‘x’] to get value of that variable
0

I'm not sure I quite understand your code but regarding your mini example, you want to do a calculation y amount of times, put the results in a list and this list will be added to another list k amount of times.

base_value = int(input("enter"))         # your x
nb_calculations = int(input("enter"))    # your y
nb_simulations = int(input("enter"))     # your k

final_list= []                           # your h

for value in range(nb_simulations):

    print('working on simulation: ', value)
    temp_list = []                       # your a
    i = 0                                # your i  


    while i < nb_calculations:
        print('working on calculation: ', i)

        new_value = base_value + 10 + i
        temp_list.append(new_value)

        print('base value: ', base_value)
        print('new value: ', new_value)
        print('temp list: ', temp_list)

        i += 1

    print('final temp list: ', temp_list)
    print('len temp list equal to nb_calculations: ', len(temp_list) == nb_calculations)

    final_list.append(temp_list)

    print('final list: ', final_list)
    print('len final list: ', len(final_list))

print('final list at the end: ', final_list)
print('len final list at the end: ', len(final_list))
print('len final list equal to nb_simulations: ', len(final_list) == nb_simulations)

Of course, temp_list will always be the same as the calculation is always the same. But the calculation is done nb_calculations amount of times, the results are put in a list and this list is added to another list nb_simulations amount of times.

Comments

0

managed to solve it, Not sure if it was the most efficient way but ill post it anyway

def run_simulation():
years, annual_spend, savings_balance, inflation_rate, interest_rate, interest_change, inflation_change, sims  = inputs()
simulations = []
loop = 0
for loop in range(0, sims):
    success = 0
    fail = 0
    initial1 = interest_rate
    initial2 = inflation_rate
    initial3 = annual_spend
    initial4 = savings_balance
    i = 0
    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)
    inflation_rate = initial1
    interest_rate = initial2
    annual_spend = initial3
    savings_balance = initial4        
    loop =+1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.