There is a list of fixed numbers y = [1,45,66,22,1988] , and there is also a variable x = random.randrange (1,1999)
I try to run the variable x in the While loop until x equals one of the numbers in the list y
y = [1,45,66,22,1988] x = 0 while ( x not in y ): print("X = ",x) x = random.randrange(0,1999)
The problem is that the cycle can stop if even x is not equal to one of the numbers in the list y . The final result was the number 305 .
How can this be fixed?
x = random.randrange(0, len(y))
, then the resultingx
is the key of the required element (y[x]
). And the element from the list will always be found in one iteration, and not over a random period of time. - BOPOH