0
import random
import time
import sys

x = input("Put a number between 1 and 100: ")
z = int(x)
if z < (0):
    sys.exit("Number too small")
if z > (100):
    sys.exit("Number too big")
y = random.randint(1, 100)

while y != z:
    print("trying again, number was", y)
    time.sleep(0.2)
    y = random.randint(1, 100)
print("Got it the number was", y)

Trying to make a randomly generated number not appear twice. Unsure how to make a number not appear twice I'm trying to keep this as flexible as possible

1 Answers1

2

Try using random.sample() which samples without replacement:

>>> import random
>>> random.sample(range(1, 101), k=20)
[98, 47, 29, 50, 19, 5, 97, 12, 35, 81, 13, 89, 16, 20, 71, 11, 24, 78, 56, 85]
>>> random.sample(range(1, 101), k=20)
[36, 41, 47, 69, 57, 98, 73, 54, 89, 86, 8, 79, 38, 17, 90, 65, 78, 30, 77, 23]
>>> random.sample(range(1, 101), k=20)
[89, 71, 38, 58, 3, 64, 66, 88, 51, 30, 80, 43, 33, 44, 26, 73, 37, 98, 19, 22]
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485