I'm doing the following code which is part of a larger program, in general, what the code is to generate a 3-digit number randomic, fulfilling this condition should not be repeated ie: 366, 225, 525 etc. .. The code validates but in some cases when the number is repeated the program crashes .. The idea is to generate the number if it is repeated again until you create it and throw the value ...

- Code: Select all
`import random`

def separar(num):

b = num % 10

e = num / 10

c = e % 10

d = e / 10

lista = [d, c, b]

return lista

def compara(l1, l2):

c1 = len(l1)

cont = 0

cont1 = 0

for i in range(c1):

for j in range(c1):

if(i == j):

if(l1[i] == l2[j]):

cont = cont + 1

elif(l1[i] == l2[j]):

cont1 = cont1 + 1

print 'Posee', cont, 'fijas'

print 'Posee', cont1, 'picas \n'

def repetido(lis1):

flat = False

c1 = len(lis1)

for i in range(c1):

for j in range(i + 1, c1):

if(lis1[i] == lis1[j]):

flat = True

return flat

def numcompu():

a = random.choice(range(100, 999))

lis1 = separar(a)

fla = repetido(lis1)

if (fla):

numcompu()

print 'grave'

else:

return a

ran = numcompu()

print ran