Bottom Page

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.
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=' ')
                print(x, end=' ')
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
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.
buran likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  variable call back into an array yamifm0f 3 223 Jun-07-2019, 02:44 PM
Last Post: heiner55
  if cond1 or cond2 or ...condN is not True asheru93 4 198 Jun-03-2019, 04:24 PM
Last Post: buran
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 108 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  split and test tweet data Jmekubo 1 124 May-08-2019, 10:48 AM
Last Post: michalmonday
  Returning True or False vs. True or None trevorkavanaugh 6 311 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  str in str == True speedskis777 7 381 Mar-27-2019, 12:15 PM
Last Post: snippsat
  file.write stops while true loop from executing in python3 boonr 3 228 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  Test Case Assertion Error moga2003 10 527 Mar-18-2019, 01:06 AM
Last Post: moga2003
  test pattern and add result in a table sam1975 1 190 Mar-05-2019, 02:41 PM
Last Post: sam1975
  'True' if 'string' has at least one uppercase letter ClassicalSoul 1 194 Feb-28-2019, 12:08 PM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)