'How to iterate code and save each output to a list
I'm just learning how to code and I've encountered a problem I can't seem to work around. I've built a strategy for a simple game of chance (similar to the Martingale strategy for those familiar) and I want to run the code n number of times and save the output of each iteration so I can aggregate the results. Note the current 'for' loop is part of my code, I tried using another 'for' loop to run the entire program n times to no avail. Thanks heaps in advance. Apologies if this is a silly question. Here is my code:
import math
import random
import heartrate
heartrate.trace(browser=True)
mult = 1.2
winChance = 0.8250
balance = 259
x = 0
for i in range (0,500):
bet1 = balance/259
balance = balance-bet1
print(balance)
n = random.random()
if n > winChance:
bet2 = (bet1*(1/(mult-1)))+bet1
balance = balance-bet2
n = random.random()
if n > winChance:
bet3 = ((bet1+bet2)*(1/(mult-1)))+bet1
balance = balance-bet3
n = random.random()
if n > winChance:
bet4 = ((bet1+bet2+bet3)*(1/(mult-1)))+bet1
balance = balance-bet4
n = random.random()
if n > winChance:
bet5 = ((bet1+bet2+bet3+bet4)*(1/(mult-1)))+bet1
balance = balance-bet5
print("end")
break
else:
balance = balance = bet4*mult
else:
balance = balance + bet3*mult
else:
balance = balance + bet2*mult
else:
balance = balance + bet1*mult
Solution 1:[1]
If I understand the question and code correctly (doubtful), this change would do what you ask for: Running your original code N times (with N = 10):
$ diff -u iterate.py.orig iterate.py
--- iterate.py.orig 2022-03-20 13:02:54.010642003 +0100
+++ iterate.py 2022-03-20 13:17:35.368615800 +0100
@@ -5,10 +5,12 @@
mult = 1.2
winChance = 0.8250
+end_balances = []
-balance = 259
-x = 0
-for i in range (0,500):
+for _ in range(10):
+ balance = 259
+ x = 0
+ for i in range (0,500):
bet1 = balance/259
balance = balance-bet1
print(balance)
@@ -35,7 +37,7 @@
break
else:
- balance = balance = bet4*mult
+ balance = balance + bet4*mult
else:
balance = balance + bet3*mult
@@ -47,3 +49,6 @@
else:
balance = balance + bet1*mult
+ end_balances.append(balance)
+
+print(end_balances)
Here is the full code:
import math
import random
import heartrate
heartrate.trace(browser=True)
mult = 1.2
winChance = 0.8250
end_balances = []
for _ in range(10):
balance = 259
x = 0
for i in range (0,500):
bet1 = balance/259
balance = balance-bet1
print(balance)
n = random.random()
if n > winChance:
bet2 = (bet1*(1/(mult-1)))+bet1
balance = balance-bet2
n = random.random()
if n > winChance:
bet3 = ((bet1+bet2)*(1/(mult-1)))+bet1
balance = balance-bet3
n = random.random()
if n > winChance:
bet4 = ((bet1+bet2+bet3)*(1/(mult-1)))+bet1
balance = balance-bet4
n = random.random()
if n > winChance:
bet5 = ((bet1+bet2+bet3+bet4)*(1/(mult-1)))+bet1
balance = balance-bet5
print("end")
break
else:
balance = balance + bet4*mult
else:
balance = balance + bet3*mult
else:
balance = balance + bet2*mult
else:
balance = balance + bet1*mult
end_balances.append(balance)
print(end_balances)
Note that you normally would extract the inner loop into a separate function.
Edit: Fixed the typo in the innermost else as well.
Solution 2:[2]
You can start by creating an empty list such as:
balance_end = []
And then appending the final balance after each iteration (I have fixed the first else which had a typo as well), such as:
else:
balance = balance + bet4*mult
balance_end.append(balance)
else:
balance = balance + bet3*mult
balance_end.append(balance)
else:
balance = balance + bet2*mult
balance_end.append(balance)
else:
balance = balance + bet1*mult
balance_end.append(balance)
Finally, you can safely assume that the balance_end index will match the number of iteration - 1 and the value, is the corresponding value of said iteration.
Solution 3:[3]
Here's my attempt to simplify things (to be honest, the code is quite convoluted and difficult to understand):
def bet(win_chance, previous_bets, mult):
if random.random() > win_chance:
this_bet = -1 * ((sum(previous_bets)*(1/(mult-1)))+previous_bets[0])
else:
this_bet = previous_bets[-1]*mult
return this_bet
# initialise variables
mult = 1.2
win_chance = 0.8250
balance = 259
for i in range(500):
previous_bets = []
previous_bets.append(balance/259)
balance -= previous_bets[0]
for i in range(5):
lost = bet(win_chance, previous_bets, mult)
balance += previous_bets[-1]
if not lost:
break
else:
# lost streak after 5 attempts
print('end')
print(balance)
Solution 4:[4]
create a list and append balance values to it
import math
import random
import heartrate
heartrate.trace(browser=True)
mult = 1.2
winChance = 0.8250
balance = 259
x = 0
values = list()
for i in range (0,500):
stop = False
bet1 = balance/259
balance = balance-bet1
print(balance)
n = random.random()
if n > winChance:
bet2 = (bet1*(1/(mult-1)))+bet1
balance = balance-bet2
n = random.random()
if n > winChance:
bet3 = ((bet1+bet2)*(1/(mult-1)))+bet1
balance = balance-bet3
n = random.random()
if n > winChance:
bet4 = ((bet1+bet2+bet3)*(1/(mult-1)))+bet1
balance = balance-bet4
n = random.random()
if n > winChance:
bet5 = ((bet1+bet2+bet3+bet4)*(1/(mult-1)))+bet1
balance = balance-bet5
print("end")
stop = True
else:
balance = balance = bet4*mult
else:
balance = balance + bet3*mult
else:
balance = balance + bet2*mult
else:
balance = balance + bet1*mult
values.append(balance)
if stop:
break
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bluehorn |
| Solution 2 | Celius Stingher |
| Solution 3 | DocZerø |
| Solution 4 | Udesh Ranjan |
