-1

I am trying to take a random number from a list that goes from 0 to 9999. When I get that number, I want to compare it to a 4 digit number. Then, if it doesn't work, I want to REMOVE that random number from the list of 0 to 9999 e.g. it picks 9999 randomly and it compares to 3129 so it deletes it from the list and tries again list (range (0000, 9998))

lis = list(range(0000,9999))
import random
num = random.choice(lis)
while int(num) < pin or int(num) > pin:
    print(num)
    num = random.choice(lis)
    count = count + 1
    lis = lis.remove(num)
print('Got your pin in ' + str(count))
from time import sleep
sleep(4)
Makyen
  • 31,849
  • 12
  • 86
  • 121
Zac
  • 1
  • 2

3 Answers3

-1
 lis = lis.remove(num) 

will set your list to 'None' because the remove method does not return anything. So just using

 lis.remove(num) 

might solve your problem - whatever your problem is. Your post doesn't really contain a clear question.

uwain12345
  • 356
  • 2
  • 21
-1
import random
comp_number = 3129
def get_give_number_from_random_list(comp_number):
    numbers_list = list(range(9999))
    default = True
    while default:
        random_number = random.choice(numbers_list)
        if comp_number == random_number:
            print('Found the {} Number at index {}'.format(comp_number,numbers_list.index(random_number)))
            default = False
        else:
            numbers_list.remove(random_number)       
get_give_number_from_random_list(comp_number)
>>>Found the 3129 Number at index 2197
Veera Balla Deva
  • 790
  • 6
  • 19
-1

I would also like to point out that your code does not take into account 0000 to 0999 combinations.

This is because python will simply count from 0 to 9999

You are also including single, double and triple numbers eg 7, 99, 167

You can prepare the list a little better and search using strings rather than int.

pin = '0001' # Search using a string
lis = ['{0:04}'.format(num) for num in range(0, 10000)] #use string foramtting to add the 0's
lis = [x for x in lis if len(str(x)) == 4] # take out any numbers that are less than 4 digits long
print(lis[0:10]) #check the first 10 items in the list
johnashu
  • 2,167
  • 4
  • 19
  • 44