Well, it's good that you go into programming.
Solution 1
I present a small code that, although it is not the best, I think it can help you understand the logic better.
print("Hola, a continuación ingresa los 5 datos")
user_inputs = []
for i in range(1, 6):
user_inputs.append(input("Ingresa el dato número {}: ".format(i)))
unique_user_inputs = []
for i in user_inputs:
exists = 0
for j in user_inputs:
if j == i:
if exists == 1:
break
else:
exists += 1
if (exists < 2):
unique_user_inputs.append(i)
print("Los elementos únicos son: {}".format(unique_user_inputs))
Now I explain the code:
for i in range(1, 6):
user_inputs.append(input("Ingresa el dato número {}: ".format(i)))
The range function receives 2 essential parameters:
Where it starts (starting from there, if omitted it is understood that this is 0)
Where it ends (ending a number before, if it is 6, ends in 5)
There is a third optional parameter, which is the jump, of how much it goes (1 in 1, 2 in 2 ....)
The {}
and .format
, is the string interpolation , which is basically replacing {}
with the parameters of format
This for checks if there are repeated elements and the only ones, puts them to another list
for i in user_inputs:
exists = 0
for j in user_inputs:
# si el elemento es el mismo
if j == i:
# si el elemento ya se había encontrado antes
if exists == 1:
break
# si el elemento no ha sido encontrado
else:
exists += 1
if (exists < 2):
unique_user_inputs.append(i)
Solution 2
Although, the code of the principle can be improved in the following way:
print("Hola, a continuación ingresa los 5 datos")
user_inputs = []
for i in range(1, 6):
temp_input = input("Ingresa el dato número {}: ".format(i))
exists = False
for j in user_inputs:
if j == temp_input:
exists = True
break
if not exists:
user_inputs.append(temp_input)
print("Los elementos únicos son: {}".format(user_inputs))
Thus, the check of repeated elements is done within the same for
used to create them, saving resources and time.
Solution 3
Finally, this can be further simplified by doing the following:
print("Hola, a continuación ingresa los 5 datos")
user_inputs = []
for i in range(1, 6):
user_inputs.append(input("Ingresa el dato número {}: ".format(i)))
print("Los elementos únicos son: {}".format(set(user_inputs)))
This last method seems to me to be the fastest, but it does not have the algorithmic part that I think is what you are looking for.
I hope I have helped you.