【问题标题】:Python: How to prevent a randomly generated number from appearing twicePython: How to prevent a randomly generated number from appearing twice
【发布时间】:2022-12-02 01:07:03
【问题描述】:
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

【问题讨论】:

    标签: python list random numbers


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-02
      • 1970-01-01
      • 2022-05-17
      • 2022-12-28
      • 2022-12-19
      • 2022-11-09
      • 2022-12-02
      相关资源
      最近更新 更多