Input in lists

0

I have to receive input (10 integers) from the user and put them in a list but not allow repeated numbers to be entered. I tried but the program if you see that the numbers are repeated does not enter any number

v = [ ]

for i in range(0,10):
x = int(input("Escreva um numero: "))
for i in v:
    if x == i:
        print("Nao pode escrever esse numero.")
    else:
        v.append(x)
print(v)
    
asked by anonymous 08.01.2018 / 19:56

1 answer

0

The range(0,10) you have will ask 10 numbers but if the person puts a repeated number, you will end up not asking for enough numbers because the repeated numbers were discarded.

It is best to turn for into while that asks for numbers until you have the amount of elements you want:

v = []
while len(v) < 10:
    x = int(input("Escreva um numero: "))
    if x in v:
        print("Nao pode escrever esse numero.")
    else:
        v.append(x)

print(v)

Example on Ideone

    
08.01.2018 / 20:34