How to make loop go back to the beginning if test evaluaes to True. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding) +--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help) +--- Thread: How to make loop go back to the beginning if test evaluaes to True. (/Thread-How-to-make-loop-go-back-to-the-beginning-if-test-evaluaes-to-True) How to make loop go back to the beginning if test evaluaes to True. - FWendeburg - Feb-13-2019 Hi, im trying to make a program that generates random numbers using the randint function. With this function I want to generate numbers but they can't be repeated. ``` def n_nonrepeated(q_of_nums, minimum_number, maximum_number): """Generates odd and even numbers, they can't be repeated.""" printed_numbers = [] for i in list(range(q_of_nums)): x = randint(minimum_number, maximum_number) if x in printed_numbers: x = randint(minimum_number, maximum_number) print(x, end=' ') else: print(x, end=' ') printed_numbers.append(x) ```As you can see in the code the best thing that I thought of doing was adding a test that generates the number again if the number that as going to be printed has already been printed. This test only works one time as if one number is in printed_numbers it runs the code and generates another umber but then if that number is repeated it doesn't check again. So i wanted to know if it's possible to make the program return to the for loop so if the number generated has already been printed it has to generate another one and so on until it prints the requested quantity of numbers without repeating one. Thanks for your help in advance. :D RE: How to make loop go back to the beginning if test evaluaes to True. - stullis - Feb-13-2019 Use a set instead of a list: ```def n_nonrepeated(q_of_nums, minimum_number, maximum_number): """Generates odd and even numbers, they can't be repeated.""" printed_numbers = set() while len(printed_numbers) < q_of_nums: printed_numbers.add(randint(minimum_number, maximum_number)) return printed_numbers ```Please note that there is the possibility of an infinite loop with this. You may want to add some more logic to prevent that.