Background/context: This program randomly selects predefined items ("Names" as strings) according to 2 rules:
#Rule 1: Each name/item in the list can only be selected once.
#Rule 2: Single item exclusion cases for all random selections such that a person cannot select themselves.
This program serves its intended purpose, but I'm seeking ways to improve the "logic" in the program such that the Exception case cannot occur in the first place and/or ways to reduce the lines of code.
Code example below:
#Rule 1: Each name/item in the list can only be selected once.
#Rule 2: Single item exclusion cases for all random selections such that a person cannot select themselves.
import random
#Function that loops through a list, randomly selecting 1 name/item from the list until the list is empty.
def choice_excluding(lst, exception):
if len(lst) > 0:
possible_choices = [v for v in lst if v != exception]
return random.choice(possible_choices)
#Exception handling function for line 60, there is a case where "Morgan" can select themself against the rules of the program.
def choice_debug(lstdebug):
return random.choice(lstdebug)
#Primary list
lst = ["Hunter", "Alena", "Derrell", "Christian", "Grant", "Audrey", "Elyas", "Loelle", "Morgan"]
#List for exception handling
lstdebug = ["Hunter", "Alena", "Derrell", "Christian", "Grant", "Audrey", "Elyas", "Loelle"]
#String variable for exception handling
alternate_choice = "Morgan"
#Print random names from the list until it's empty while excluding a specific item/name each time and removing the selected item/name from the list each time so it can't be selected again.
choice1 = choice_excluding(lst, "Hunter")
print("Hunter your assingment is " + choice1)
lst.remove(choice1)
choice2 = choice_excluding(lst, "Alena")
print("Alena your assingment is " + choice2)
lst.remove(choice2)
choice3 = choice_excluding(lst, "Derrell")
print("Derrell your assingment is " + choice3)
lst.remove(choice3)
choice4 = choice_excluding(lst, "Christian")
print("Christian your assingment is " + choice4)
lst.remove(choice4)
choice5 = choice_excluding(lst, "Grant")
print("Grant your assingment is " + choice5)
lst.remove(choice5)
choice6 = choice_excluding(lst, "Audrey")
print("Audrey your assingment is " + choice6)
lst.remove(choice6)
choice7 = choice_excluding(lst, "Elyas")
print("Elyas your assingment is " + choice7)
lst.remove(choice7)
choice8 = choice_excluding(lst, "Loelle")
print("Loelle your assingment is " + choice8)
lst.remove(choice8)
#Exception handling, programatically swapping choice9 in the case where Morgan can select themself by creating a new variable choice10. Morgan's new choice is selected from all other items/names in a new random pick then assign the name that had Morgan's choice to have "Morgan" instead.
try:
choice9 = choice_excluding(lst, "Morgan")
print("Morgan your assingment is " + choice9)
except Exception as e:
print("Oops!", e.__class__, "occurred.")
choice10 = choice_debug(lstdebug)
print("Morgan your assingment is " + choice10)
if choice1 == choice10:
print("Hunter your new assingment is " + alternate_choice)
if choice2 == choice10:
print("Alena your new assingment is " + alternate_choice)
if choice3 == choice10:
print("Derrell your new assingment is " + alternate_choice)
if choice4 == choice10:
print("Christian your new assingment is " + alternate_choice)
if choice5 == choice10:
print("Grant your new assingment is " + alternate_choice)
if choice6 == choice10:
print("Audrey your new assingment is " + alternate_choice)
if choice7 == choice10:
print("Elyas your new assingment is " + alternate_choice)
if choice8 == choice10:
print("Loelle your new assingment is " + alternate_choice)