<snip>

<snip>

Thank you all for your help! I've added another condition to this program, 
namely, in a range of 60, each 'random' number can only occur 10 times. I came 
up with the following:

import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while series[-2:-1] == series[-1:] == temp:
    temp = [random.randint(1, 6)]
  while series.count(temp[0])>= 10:
    temp = [random.randint(1, 6)]
  series.extend(temp)
print series

However, this also generates gems such as:

[4, 4, 3, 6, 3, 2, 6, 3, 4, 3, 4, 1, 4, 2, 4, 3, 4, 4, 6, 2, 1, 5, 5, 6, 5, 6, 
5, 6, 3, 3, 1, 6, 6, 4, 1, 5, 2, 6, 6, 4, 2, 5, 3, 1, 5, 5, 2, 1, _2_, _2_, 
_2_, 3, 3, 2, 1, 5, 1, 1, 5, 1]

[1, 4, 3, 1, 5, 3, 5, 1, 5, 6, 5, 3, 2, 6, 1, 4, 1, 5, 5, 2, 6, 2, 4, 1, 3, 1, 
2, 1, 1, 3, 1, 6, 6, 3, 2, 3, 6, 4, 5, 6, 5, 6, 3, 6, 2, 4, 5, 3, 3, 4, 6, _4_, 
_4_, _4_, 5, 4, _2_, _2_, _2_, _2_]

I thought this needed to become the following:

import random
series = []
for i in range(60):
  temp = [random.randint(1, 6)]
  print "Temp", i, ":", temp
  while series[-2:-1] == series[-1:] == series:
    if series.count(temp[0])>= 10:
      temp = [random.randint(1, 6)]
  series.extend(temp)
print series

But this just hangs whenever the while clause matches. I'm not sure what I'm 
doing wrong here. I do know the random.shuffle() function, but I can't put my 
conditions in there.

I assume the '== series:' is a typo, and should be '== temp'. Otherwise I don't 
see how this matches, unless series = [] (which means the while clause always 
matches, right at the start. Perhaps that actually is what you're seeing?).
That's probably the disadvantage of using a list to avoid an IndexError (sorry 
Steven; I would have just caught the IndexError, or have an 'if i<  2: 
continue' statement in there.)

Yes of course :) That's a typo on my part. I came up with the following which I think works as well?

import random
reeks = []
for i in range(60):
  temp = [random.randint(1, 6)]
  while reeks[-2:-1] == reeks[-1:] == temp:
    temp = [random.randint(1, 6)]
    if reeks.count(temp[0]) >= 10:
      temp = [random.randint(1, 6)]
  reeks.extend(temp)
print reeks


As suggestion for avoiding occurrences of>  10 times: use a dict, where the random 
numbers become the keys and you add +1 every time to d[temp]. Checking if d[temp]> 
 10 is then very easy.

I'll check dicts as well.  Thank you! :)
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to