List element Zera after loop for

Asked

Viewed 88 times

3

I created an array and filled it with 100 [0] (because I don’t know a better way to do it) and after that I wanted to change these values using a loop for within another loop for. But what happens is that after the inside loop, when it comes out, all the values are reset again...

Here is the code (there are three prints there that you can see that inside the for it changes to the value I want but when the for, he goes back to being zero):

mediasCa = []

for k in range(0,itera):
    mediasCa.append(0)

for j in range(0,99):
    a = quant
    print(mediasCa[j])
    for i in range(0,itera):
        bola = randint(1,quant)
        if bola < a:
            a -= 1
        else:
            a += 1
    print(mediasCa[i])
    mediasCa[i] = mediasCa[i] + a
    print(mediasCa[i])
  • I already found the error '-', I’m iterating the vector out of the right loop... missing a tab =p

  • Post the correct code as response.

  • I don’t know how to edit right using markdown, but: mediasCa = []&#xA; &#xA; for k in range(0,itera):&#xA; mediasCa.append(0)&#xA; &#xA; &#xA; for j in range(0,99):&#xA; aux = []&#xA; a = quant&#xA; &#xA; for i in range(0,itera):&#xA; bola = randint(1,quant)&#xA; if bola < a:&#xA; a -= 1&#xA; else:&#xA; a += 1&#xA; mediasCa[i] = mediasCa[i] + a

  • As reply Peter, here is comment.

  • Sorry, first time here.

  • No problem is that when you’ve found the solution it’s always good to post the answer.

Show 1 more comment

1 answer

3

The error was syntax missing a TAB.

mediasCa = []    
for k in range(0,itera):
    mediasCa.append(0)
for j in range(0,99):
    aux = []
    a = quant
    for i in range(0,itera):
        bola = randint(1,quant)
        if bola < a:
            a -= 1
        else:
            a += 1
        mediasCa[i] = mediasCa[i] + a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.