Python Forum
How to make loop go back to the beginning if test evaluaes to True.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make loop go back to the beginning if test evaluaes to True.
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to automate loop test check on Network device jpc230 1 544 Oct-09-2023, 09:54 PM
Last Post: Larz60+
  Program doesnt return beginning bilisim19 2 897 Feb-15-2023, 06:23 PM
Last Post: Larz60+
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  "while" loop is looping back too early mangurian 1 1,242 Jan-28-2022, 09:15 AM
Last Post: ibreeden
  How to make for loop display on 1 Line Extra 3 1,375 Jan-12-2022, 09:29 PM
Last Post: Extra
  List of dataframe values beginning with x,y or z glidecode 3 1,890 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Looking for discord bot to make loop ping for address ip tinkode 0 1,789 Jul-26-2021, 03:51 PM
Last Post: tinkode
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,064 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to make a test data file for the full length of definition? MDRI 6 3,479 Apr-16-2021, 01:47 AM
Last Post: MDRI
  How to start the program from the beginning. iamaghost 5 2,872 Feb-23-2021, 03:40 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020