May-24-2019, 06:49 PM
(May-24-2019, 05:58 PM)ichabod801 Wrote: Some tips on your code: since everything in whole_list is an int, you don't need to convert them to an in on line 16. Also, you are not really using status. You break out of the while loop, so it doesn't end because you change status to True. And when you check status on line 19 it is necessarily False. The only time it is True is right before you break out of the loop that line 19 is in. I would change line 13 towhile len(selection_list) < 5:
. Then you can remove lines 22-25 and remove status from line 19.
Here's a not-so-short form of my solution, if it helps:
valid = [] for number in whole_list: if number not in outcast_list: valid.append(number) selection_list = random.sample(valid, 5)
Thanks for the tips. It actually took me a while to understand why I shouldn't use boolean (i.e. status) to check the validity. Just to understand more on using boolean to check for status, can you please share with me more by quoting an example (with code snippet)?