dimanche 6 juin 2021

Using a for if break statement to append values in a list

I'm working with loops and I got a question, so I have a simple for loop that append random values between 0 and 1, basically what I need is to keep adding values until a certain number of 0 or 1 appear, for example:

iteration 1 = [0]
iteration 2 = [0,1]
...
iteration 6 = [0,1,1,1,1,1] #Here appear five 1's so I break the loop (it can be another number not necessarily 5 ones or 5 zeros)

The for loop is pretty simple:

import numpy as np

value = []
for i in range(5):
    value.append(np.random.randint(0, 2))
    print(value)

And here is what I tried to break the loop using a if-break statement

value = []
for i in range(5):
    if value == value:
        value.append(np.random.randint(0, 2))
    else
        break

    print(value) 

My biggest problem is that I don't know how to specify the codition that if a certain numbers of 0 or 1 appears break the loop, so I was hoping you can point me to a right direction. Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire