What we must do in this matter is proibir
the insertion of any element, the value of which corresponds to the value of any other element which já exista na lista
. For this I implemented the following code below.
To use this code we must execute it, then enter the amount of values we want to enter in the list. Then insert each of the values. Note that if a value is entered that already exists within the list the application will inform that that value already exists and request another. Note that, the application will only proceed with its execution if you insert different values to those that already exist within the list.
n = int(input('Digite a quantidade de valores: '))
valores = list()
for c in range(1, n + 1):
x = int(input(f'Digite o {c}ª valor: '))
while x in valores:
print('\033[31mO valor já existe! Digite outro valor!\033[m')
x = int(input(f'Digite o {c}ª valor: '))
valores.append(x)
print()
print(f'\033[32mA lista gerada foi: {valores}\033[m')
See here the functioning of the algorithm.
If you think it best to put the list in ascending order you can use the following code...
n = int(input('Digite a quantidade de valores: '))
valores = list()
for c in range(1, n + 1):
x = int(input(f'Digite o {c}ª valor: '))
while x in valores:
print('\033[31mO valor já existe! Digite outro valor!\033[m')
x = int(input(f'Digite o {c}ª valor: '))
valores.append(x)
valores.sort()
print()
print(f'\033[32mA lista gerada foi: {valores}\033[m')
See here the functioning of the algorithm.