mercredi 27 mai 2015

Random's randint won't work in a for-loop

I'm trying to create a list with random length filled with lists of random lengths by using this code:

import random

solitaire = [None]*(random.randint(1,5)) 

for pile in solitaire:
    number = random.randint(0, 10)
    solitaire.append(number)    

print(solitaire)

Easy enough I though but when I ran this code my powershell window froze as it was expecting an input or something, I had to cancel the script with ctr+c and then got the message:

Traceback (most recent call last):
File "sparakod.py", line 254, in <module>
number = random.randint(0, 10)
File "C:\Python34\lib\random.py", line 218, in randint
return self.randrange(a, b+1)
File "C:\Python34\lib\random.py", line 170, in randrange
def randrange(self, start, stop=None, step=1, _int=int):
KeyboardInterrupt

What does this mean? Why won't the code run?

 number = random.randint(0, 10)

Seems to work just fine so why won't it inside the for-loop?

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire