1
import random
warrior = 'Warrior'
mage = 'Mage'
warlock = 'Warlock'
paladin = 'Paladin'
firstList = [warrior, mage, warlock, paladin]
print(firstList[0], firstList[1], firstList[2], firstList[3])
firstNumber = random.randint(0, 3)
firstList[firstNumber] = '*taken*'
print(firstList[0], firstList[1], firstList[2], firstList[3])
secondList = [paladin, warlock, mage, warrior]
print(secondList[0], secondList[1], secondList[2], secondList[3])

Let's say the random number picks 0 I realized that the 9th line actually just changes the list entry to "taken", instead of changing the 'warrior' variable that entry refers to into "taken". Is there any way to do that?

Forgive the WoW references.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124

4 Answers4

3

You want to use dictionaries instead:

taken = {'warrior': False, 'mage': False, 'warlock': False, 'paladin': False}
names = {'warrior': 'Warrior', 'mage': 'Mage', 'warlock': 'Warlock', 'paladin': 'Paladin'}

or:

taken = dict.fromkeys(('warrior', 'mage', 'warlock', 'paladin'), False)
names = {character: character.capitalize() for character in taken}

then pick a random character:

from random import choice
picked = choice(list(names.keys()))
taken[picked] = True
print('Picked:', names[picked])

You really do not want to use separate local identifiers to represent a group of characters, that only complicates matters.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

You cannot do this using your current technique. I suggest using the technique in Martijn Pieters' answer.

Just to expand a tiny bit on why you can't:

Your variables are all string objects, which are immutable (as noted by Peter Wood). Immutable means cannot be changed - when you change the variable, you are making a new object. If you were to be doing the same thing with a mutable type, then this would be possible

>>> list_1 = []
>>> list_2 = []
>>> main_list = [list_1, list_2]
>>> main_list[1].append(5)
>>> print(list_2)
[5]

For more on mutability, see:
Immutable vs Mutable types
Which of these are immutable in Python?
http://stsdas.stsci.edu/pyraf/python_quick_tour.html#PyMutability

Community
  • 1
  • 1
William
  • 2,917
  • 5
  • 30
  • 47
0

Why not use an object oriented approach?

from random import choice

class Character(object):
    def __init__(self, name, taken=False):
        self.name = name
        self.taken = taken
    def get_taken(self):
        self.taken = True
    def __repr__(self):
        return self.taken and '{} (taken)'.format(self.name) or self.name


class Team(list):
    def take_someone(self, number=None):
        if number is None:
            choice(self).get_taken()
        else:
            self[number].get_taken()


myTeam = Team([Character("Warrior"),
               Character("Mage"),
               Character("Warlock")])
print myTeam

[Warrior, Mage, Warlock]

myTeam.take_someone()

print myTeam

[Warrior, Mage, Warlock (taken)]

Now you even can initialize a character as "taken", and you can either let your myTeam object choose some random character to be taken using myTeam.take_someone(), or you can take a character by index. e.g. myTeam.take_someone(0).

mawimawi
  • 4,222
  • 3
  • 33
  • 52
0

I'd go with two sets, untaken and taken, since this seems like something where order is irrelevant.

So you'd choose members and move them between sets with something like

import random
taken = set()
untaken = set(['Mage','Warlock','Warrior','Paladin'])
chosen = random.sample(untaken,1)
taken.add(chosen)
untaken.remove(chosen)
desfido
  • 787
  • 6
  • 16