samedi 27 février 2016

random.randint on lists in python

im very new to programming , and i've been cracking my head on this : i want to create a list called dasos , and fill it with 15 zeros , then i want to change the 0 to 1 in 5 random spots of the list , so it has 10 zeros and 5 ones ,here is what i tried

import random, time

dasos = []

for i in range(1, 16):
  dasos.append(0)

for k in range(1, 6):
  dasos[random.randint(0, 15)] = 1

some times i would get anywhere from 0 to 5 ones but i want exactly 5 ones , if i add a

print(dasos) 

to see my list i get :

IndexError: list assignment index out of range

thanks in advance




Aucun commentaire:

Enregistrer un commentaire