May-24-2019, 07:29 PM
As an example, here's your for loop redone:
That works here, because the termination condition is simple. If you have to do some processing to get the condition, so you use a conditional with a break, like you did:
while len(selection_list) < 5: # Note that if you are just selecting one item, random.choice returns the item, not a list i = random.choice(whole_list) # We no longer need to check status, just outcasts and what's already been selected. if (i not in outcast_list) and (i not in selection_list): selection_list.append(i) # We no longer need to check for a break, because the while loop will do thatRight after every append, it goes back to the while statement. The while statement checks the current length. If we don't have five items yet, it does the loop again. If we have five or more, it skips to the end of the loop and continues on with the rest of the program. The loop is directly testing the termination condition, rather than getting it through the proxy of status.
That works here, because the termination condition is simple. If you have to do some processing to get the condition, so you use a conditional with a break, like you did:
while True: choice = input('Please select a menu item: ') if is_valid(choice): break print('That is not a valid selection.')Here, we need to get the user input, and also check it for validity, before we can be sure it's time to exit the loop. But again, we don't need the proxy of status, we just check the validity of the choice, and then exit the loop if the choice is valid.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures