-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
20
  • 1
    nothing to do with pandas, removed the tag Commented May 28, 2020 at 16:26
  • 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". Commented May 28, 2020 at 16:29
  • 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. Commented May 28, 2020 at 16:38
  • 1
    You still haven't indented the inner loop. Commented May 28, 2020 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): Commented May 28, 2020 at 16:45

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

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.