Итак, я написал код, который генерирует случайный список со случайным количеством значений.Затем спрашивает пользователя, какой номер он ищет, и если его в списке, он скажет пользователю, в какой позиции в списке находится номер.
import random
a = [random.randint(1, 20) for i in range(random.randint(8, 30))]
a.sort()
print(a)
def askUser():
n = input("What number are you looking for?")
while not n.isdigit():
n = input("What number are you looking for?")
n = int(n)
s = 0
for numbers in a:
if numbers == n:
s += 1
print("Number", n, "is located in the list and the position is:", (a.index(n)+1))
# Something here to skip this index next time it goes through the loop
else:
pass
if s == 0:
print("Your number could not be found")
askUser()
Я хотел бы добавить что-то, что будетпропустите найденный индекс в первый раз, а затем найдите индекс дубликата, если он есть.
Текущий результат
[2, 4, 8, 9, 10, 10, 16, 19, 20, 20]
What number are you looking for?20
Number 20 is located in the list and the position is: 9
Number 20 is located in the list and the position is: 9
Желаемый результат
[2, 4, 8, 9, 10, 10, 16, 19, 20, 20]
What number are you looking for?20
Number 20 is located in the list and the position is: 9
Number 20 is located in the list and the position is: 10