0

enter image description hereI'm trying to do something to learn numbers in English. but I don't want the results to be repeated. Example: 44, 44, 55, 55 is not rewritten, each number is written only once and the program is reset at the last number, I am working in Python. Note: I am using Sublime Text

import random
import time

while True:    
    number = random.randint(1,10)
    print(number)

    time.sleep(5)
  • 3
    Put the numbers in a list and shuffle the list? – 001 Jun 03 '22 at 18:25
  • Welcome to Stack Overflow. What exactly is the question? Are you looking for an *approach* to solving the problem, or do you need help *writing the specific code*? If you want code, you should first figure out the approach, and then try to write it, and figure out exactly what part you aren't able to write yourself. If you are looking for an approach, then it does not matter very much what programming language you are using. – Karl Knechtel Jun 03 '22 at 18:30

1 Answers1

0

Use either random.sample or random.shuffle to produce the shuffled values up front, then iterate over them one by one:

for number in random.sample(range(1, 11), 10):
    print(number, flush=True)
    time.sleep(5)

or:

values = list(range(1, 11))
random.shuffle(values)
for number in values:
    print(number, flush=True)
    time.sleep(5)

I added the flush=True to ensure the number is printed immediately, even when stdout is in block-buffering mode, rather than buffering them all up and only outputting anything when the loop finishes.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271