Reputation: 13
As the title says: how to you remove a random item from a list? I am making text based game, and I have a list in which I want to randomly take an item from and then remove it from the list, as seen below:
Deck = ['Lumina, Lighsworn Summoner', 'Lumina, Lighsworn Summoner', 'Judgment Dragon', 'Judgment Dragon', 'Judgment Dragon', 'Jain, Lightsworn Paladin', 'Ehren, Lightsworn Monk', 'Lyla, Lightsworn Sorceress', 'Lyla, Lightsworn Sorceress', 'Ryko, Lighsworn Hunter', 'Ryko, Lighsworn Hunter', 'Ryko, Lighsworn Hunter', 'Celestia, Lightsworn Angel', 'Aurkus, Lightsworn Druid', 'Garoth, Lightsworn Warrior', 'Garoth, Lightsworn Warrior', 'Lightray Gearfried', 'Lightray Gearfried', 'Lightray Gearfried', 'Lightray Daedalus', 'Lightray Daedalus', 'Lightray Daedalus', 'Lightray Diabolos', 'Lightray Diabolos', 'Lightray Diabolos', 'Sephylon, the Ultimate Timelord', 'Sephylon, the Ultimate Timelord', 'Sephylon, the Ultimate Timelord', 'Card Trooper', 'Card Trooper', 'Honest', 'Gorz the Emissary of Darkness', 'Necro Gardna', 'Necro Gardna', 'Necro Gardna', 'Charge of the Light Brigade', 'Solar Recharge', 'Solar Recharge', 'Solar Recharge', 'Beckoning Light', 'Beckoning Light']
loop = 1
while loop == 1:
option = raw_input()
if option == 'draw':
newcard = random.sample(Deck, 1)
print newcard
Deck.remove(newcard)
However, when I try the in-game 'command' "draw", I always get this output and list-related error:
draw
['Judgment Dragon']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "YGOGame.py", line 183, in <module>
Deck.remove(newcard)
ValueError: list.remove(x): x not in list
Any help is appreciated.
Upvotes: 1
Views: 6626
Reputation: 1121972
newcard
is a list (you used random.sample(Deck, 1)
, which returns a list); use:
Deck.remove(newcard[0])
or use random.choice()
to pick one element:
newcard = random.choice(Deck)
Upvotes: 7