Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to make loop go back to the beginning if test evaluaes to True. FWendeburg Programmer named Tim Posts: 17 Threads: 11 Joined: Jan 2019 Reputation: 1 Likes received: 0 #1 Feb-13-2019, 12:34 AM 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 stullis Minister of Silly Walks Posts: 382 Threads: 1 Joined: Sep 2018 Reputation: 45 Likes received: 63 #2 Feb-13-2019, 01:26 AM 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 « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post How to write test cases for a init function by Unit test in python? binhduonggttn 2 169 Feb-24-2020, 12:06 PM Last Post: Larz60+ How to write test cases by Unit test for database configuration file? binhduonggttn 0 61 Feb-18-2020, 08:03 AM Last Post: binhduonggttn For loop to make it easier Amirdst 0 77 Feb-18-2020, 02:24 AM Last Post: Amirdst Do break operators turn while loop conditions from True to False? Drone4four 5 324 Oct-24-2019, 07:11 PM Last Post: newbieAuggie2019 How to use while true/while loop on python christing 4 285 Oct-08-2019, 08:02 AM Last Post: perfringo out and back to loop vlk 2 183 Oct-07-2019, 06:26 PM Last Post: vlk Returning True or False vs. True or None trevorkavanaugh 6 697 Apr-04-2019, 08:42 AM Last Post: DeaD_EyE file.write stops while true loop from executing in python3 boonr 3 505 Mar-25-2019, 12:50 PM Last Post: ichabod801 Returning true or false in a for loop bbop1232012 3 555 Nov-22-2018, 04:44 PM Last Post: bbop1232012 Get True of false outside a loop morgandebray 2 557 Aug-09-2018, 12:39 PM Last Post: morgandebray

Forum Jump:

Users browsing this thread: 1 Guest(s)