I'm new to coding but is being asked to create a simulation of a 10-year experiment 1000 times. I have it at a number lower than 1000 to speed up the testing process. This is a partial copy of my code, I can get the other parameters of the task to work but instead of stopping at and restarting for every 10-years, it seems to accumulate the results of the previous years'.
For example, the code is supposed to compound money earned the year following a 'Success,' while I can get it to compound, my code seems to compound into year 11 and 12 instead of stopping at 10 and essentially restarting at year 1.
I tried .count() to keep track of how many elements I'm iterating through and also tried the while xyz function but I can't seem to get either to work.
for sim in range(5):
for yr in range(10):
experiment = "Success" if np.random.random() <= 0.1 else "Failure"
expense = 25000
margin = 0
results1.append(experiment)
expenses1.append(expense)
margins1.append(margin)
iter = 0
if iter < 10:
for i in range(len(results1)):
if i + 1 < len(results1) and i - 1 >= 0:
if results1[i] == 'Success':
expenses1[i + 1] = 0
margins1[i + 1] = 10000
if results1[i - 1] == 'Success':
expenses1[i] = 0
if margins1[i] != 10000:
margins1[i] = 10000
if expenses1[i - 1] == 0:
expenses1[i] = 0
expenses1[i + 1] = 0
if margins1[i] >= 10000:
margins1[i + 1] = margins1[i] * 1.2
iter += 1
else:
continue
iter = 0
all_data1 = zip(results1, expenses1, margins1)
df1 = pd.DataFrame(all_data1, columns=["Results", "R&D", "Margins"])