2
I have the algorithm below, and on the line while lista[i]==lista[i-1]:
is returning the error:
index out of range
import random
n1=0
n2=0
n1=int(input('Insira um numero para inicio'))
n2=int(input('Insira um numero para limite'))
i=1
lista=[]
for i in [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]:
lista.append(random.randint(n1,n2))
i=1
count=0
print(lista[i-1])
lista.sort()
while i<=15:
while lista[i]==lista[i-1]:
lista.pop(i)
lista.insert(i,random.randint(n1,n2))
i+=1
print(lista)
Usually when you start a loop, the increasing variable always starts at 0, in the snippet
lista[i-1]
you’re making0 - 1
, so the result is-1
then he can’t find theindex
array, what you intend to do in this code?– Franck Costa
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero