-1

I would like to execute a loop in a loop, but when I try to do that I catch a wrong calculation comparing to a singular loop.

My code with a singular loop that works:

betresult = []

#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15


for i in range(numofbets):
    #random list: 1 - win, 0 - loss
    test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
    betresult.append(test)

    #number of win and loss
    winbets = np.sum(betresult)
    lossbets = numofbets - winbets

    #sum of win and loss
    winsum = winbets * win
    losssum = lossbets * loss

    #final sum
    result = budget - (winsum + losssum)

print(result)

And I would like to put that code in a new loop, but if I make something obviously with while or for, I catch a wrong calculation.

With a singular loop I usually get one number between 1100 and 1800.

Code with a wrong calculation:

betresult = []

#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15

for i in range(10):   
    for j in range(numofbets):
        #random list: 1 - win, 0 - loss
        test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
        betresult.append(test)

        #number of win and loss
        winbets = np.sum(betresult)
        lossbets = numofbets - winbets

        #sum of win and loss
        winsum = winbets * win
        losssum = lossbets * loss

        #final sum
        result = budget - (winsum + losssum)

     print(result)

With a double loop I usually get a number under 0. And the bigger a loop range, the bigger a number under 0.

An example of wrong execution (calculation):

1470.0
7.5
-1590.0
-3007.5
-4447.5
-5910.0
-7440.0
-8835.0
-10410.0
-11985.0
  • 1
    nothing to do with pandas, removed the tag – anky May 28 '20 at 16:26
  • @anky thanks a lot. – btnvstusrnm May 28 '20 at 16:28
  • 2
    We can't help with code that we can't see. Add the code that attempts to execute a loop within a loop. And could you explain what you mean by "I catch a wrong calculation". – jarmod May 28 '20 at 16:29
  • Check this link out to get there – Thingamabobs May 28 '20 at 16:32
  • 1
    python is sensative to indentation. your second for loop needs to be indented withing the first one. Meaning the second for loop statement needs to be indented, and anything inside it should be indented twice. Anything indented once is inside only the first for loop, anything indented twice is inside the second loop (which itself is inside the first one). Also don't use "i" twice as your variable that goes over the range, this will cause it to overwrite itself and cause problems. – Oha Noch May 28 '20 at 16:38
  • @jarmod Thanks. I've updated the post.Take a look, please. – btnvstusrnm May 28 '20 at 16:39
  • 1
    You still haven't indented the inner loop. – Barmar May 28 '20 at 16:43
  • 1
    If you're not using the iteration variable, the convention is to use `_`: `for _ in range(10): for _ in range(numofbets):` – Barmar May 28 '20 at 16:45
  • @OhaNoch Actually, I make right indentations (I could't do that here :D). But how can I change second "i"? I tried, but I caught an error (there is no variable "x"). – btnvstusrnm May 28 '20 at 16:45
  • 1
    You can use `for i` for the first loop and `for j` for the second loop. – Barmar May 28 '20 at 16:46
  • 1
    Paste your correctly indented code. Then put triple-backtick on the lines before and after the code. – Barmar May 28 '20 at 16:47
  • 1
    Bamar is correct, if you do not intend on using the variable that iterates over the range you should probably use "_" instead of "i". For me I usually use "i" for outer loop and "j" for the inner loop, which also works but isn't best practice if you do not intend to use these variables. How does the for loop look when you get " I tried, but I caught an error (there is no variable "x")"? – Oha Noch May 28 '20 at 16:52
  • @barmar Thanks. I've put the write code. And I tried to change variable, but it still works wrong (I've also put a wrong result) – btnvstusrnm May 28 '20 at 16:52
  • @OhaNoch the different variable is already working, sorry, But I still have a core issue) – btnvstusrnm May 28 '20 at 16:54
  • 1
    You don't use the iteration variables, so they don't matter at all. – Barmar May 28 '20 at 16:56
  • 1
    you should declare the betresult list inside the first loop – DaVinci May 28 '20 at 16:57
  • 1
    @Tonystark is correct, I just noticed it as well, changing it brings a good result, i checked – Oha Noch May 28 '20 at 17:00
  • @Tonystark Yaaaas, man! You're a genius! It finally works! Can you write a full answer and I 'll mark it as write answer? – btnvstusrnm May 28 '20 at 17:02
  • 1
    @btnvstusrnm i have done it – DaVinci May 28 '20 at 17:04
  • Guys, thanks you all! Sorry for the misleading! And I hope you can rescue the reputation of my question (-2) :D Hugs! – btnvstusrnm May 28 '20 at 17:09

2 Answers2

3

The problem is the betresult is appending all the value for 10 iterations that's why you get negative answers

import numpy as np


#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15

for i in range(10):
    #declaring inside the first loop
    betresult = []
    for j in range(numofbets):
        #random list: 1 - win, 0 - loss
        test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
        betresult.append(test)

        #number of win and loss
        winbets = np.sum(betresult)
        lossbets = numofbets - winbets

        #sum of win and loss
        winsum = winbets * win
        losssum = lossbets * loss

        #final sum
        result = budget - (winsum + losssum)
    print(result)
DaVinci
  • 868
  • 1
  • 7
  • 25
3
import numpy as np
betresult = []

#main parameters
budget = 1500
numofbets = 100
win = 7.5
loss = -15
summary = 0
for i in range(80):   
    for i in range(numofbets):
        test = np.random.choice(np.arange(0, 2), p=[0.333, 0.667])
        betresult.append(test)

        winbets = np.sum(betresult)
        lossbets = numofbets - winbets

        winsum = winbets * win
        losssum = lossbets * loss

        result = budget - (winsum + losssum)
     #betresult.clear() should be outside the second loop
     betresult.clear()

print(result)

I think you need to clear your betresult each time you run this loop. Maybe you need some extra Code to calculate, depends on what you want.

DaVinci
  • 868
  • 1
  • 7
  • 25
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • 1
    I think you need to put `betresult.clear()` in the outer loop, not the inner loop. Just move `bestresult = []` to the beginning of the loop. – Barmar May 28 '20 at 17:07