I am trying to create a simple program in Python 3.3 that takes a list of four names, and randomly assigns them to another person in the list. For example, if the names were John, Aaron, Lydia, and Robin:
John goes first and selects a name. He cannot draw his own; if he does, he puts it back and draws again. Say John drew Robin's name. Robin's name would be eliminated from the pool. Next is Aaron's turn to draw. He draws John. John's name is eliminated. etc. until all names are assigned.
I created a list with four names and assigned each one a value 1-4. However, i'm running into an issue when removing from a list, saying the value does not exist.
list.remove(x) : x not in list.
it looks something like this:
def drawNames():
import random
John=1
Aaron=2
Lydia=3
Robin=4
validNames=[John, Aaron, Lydia, Robin]
nameDrawn=random.choice(validNames)
def draw():
nameDrawn=random.choice(validNames)
#John's Draw:
draw()
if nameDrawn != 1:
if nameDrawn == 2:
print("John drew: Aaron")
validNames.remove(2)
elif nameDrawn == 3:
print("John drew: Lydia")
validNames.remove(3)
elif nameDrawn == 4:
print("John drew: Robin")
validNames.remove(4)
#Aaron's Draw:
draw()
if nameDrawn !=2:
if nameDrawn ==1:
print("Aaron drew: John")
validNames.remove(1)
elif nameDrawn ==3:
print("Aaron drew: Lydia")
validNames.remove(3)
elif nameDrawn ==4:
print("Aaron drew: Robin")
validNames.remove(4)
#Lydia's Draw:
draw()
if nameDrawn !=3:
if nameDrawn ==1:
print("Lydia drew: John")
validNames.remove(1)
elif nameDrawn ==2:
print("Lydia drew: Aaron")
validNames.remove(2)
elif nameDrawn ==4:
print("Lydia drew: Robin")
validNames.remove(4)
#Robin's Draw:
draw()
if nameDrawn !=4:
if nameDrawn ==1:
print("Robin drew: John")
validNames.remove(1)
elif nameDrawn ==2:
print("Robin drew: Aaron")
validNames.remove(2)
elif nameDrawn ==3:
print("Robin drew: Lydia")
validNames.remove(3)
drawNames()
i've also tried using the names rather than the numeric values, which yielded the same error.
I also feel like this is an inefficient scheme. If you have a better suggestion I'd be much obliged.
You might have better mileage with a code like this below; it's more scalable to many names than what you've provided above.
import copy
import random
validNames=["John", "Aaron", "Lydia", "Robin"]
def drawNames(namelist,currentname):
'''
namelist: list of names to draw from
currentname: name of person doing the current draw
'''
draw_namelist = copy.copy(namelist) # make a copy to remove person drawing if needed
if currentname in draw_namelist: # check if the person drawing is in the list
draw_namelist.remove(currentname) # remove current name if in list
try:
drawn_name = random.choice(draw_namelist)
namelist.remove(drawn_name)
newnamelist = namelist
print "Drew {}".format(drawn_name)
print "New list: {}".format(newnamelist)
except:
print "Nobody for me to draw!"
drawn_name=None
newnamelist = namelist
return drawn_name, newnamelist
This can then work as follows:
In [39]: newlist=["John", "Aaron", "Lydia", "Robin"]
In [40]: name,newlist = drawNames(newlist,"Lydia")
Drew Robin
New list: ['John', 'Aaron', 'Lydia']
In [41]: name,newlist = drawNames(newlist,"John")
Drew Aaron
New list: ['John', 'Lydia']
In [42]: name,newlist = drawNames(newlist,"Aaron")
Drew John
New list: ['Lydia']
In [43]: name,newlist = drawNames(newlist,"Robin")
Drew Lydia
New list: []
You have to call the actual element, not the index in remove. Replace the numbers with the actual names of people as they exist in the list, and the code should work as intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With