0
players_list = [Ani, Paty, Felix, Alex]

class Player:

def __init__(self, name):
    self.name = name
    self.score = 0
    self.vote = 0
    self.player_hand = []
    self.choice = ''
    self.player_hand = []
            
def player_turn(self):
    print(self.name, "'s turn")


def p_vote(self):
    print(self.name, " voted")

I tried to iterate over the list, but it always gives me an error: NameError: name 'Ani' is not defined

for player in players_list:
    player = Player(str(player))

But doing all the process manually work:

Ani = Player("Ani"), etc

Is there any way that i can automate this process?

2
  • Why have you used quotes in the example that works, and not in the one that doesn't? Commented Jul 3, 2020 at 20:10
  • for obj in players_list: object(obj) = Player(obj)? Commented Jul 3, 2020 at 20:11

3 Answers 3

2

First of all the thing you should know, the players_list that you have declared are not containing strings, they are being considered as variables which you have not defined anywhere, and therefore the NameError.

Now, if you want to correct this, and if you actually intend to store objects of Player in players_list, then you can do the following:

players_list = ["Ani", "Paty", "Felix", "Alex"]

class Player:

    def __init__(self, name):
        self.name = name
        self.score = 0
        self.vote = 0
        self.player_hand = []
        self.choice = ''
        self.player_hand = []
            
    def player_turn(self):
        print(self.name, "'s turn")
    
    
    def p_vote(self):
        print(self.name, " voted")
        
for i in range(len(players_list)):
    players_list[i]=Player(players_list[i])

This will store Player objects in the list you have declared just the thing that you expect to get.

Sign up to request clarification or add additional context in comments.

Comments

0

You are having problems with the players not being defined. So players_list = [Ani, Paty, Felix, Alex] will throw an error because the objects Ani, Paty, Felizx, and Alex do not exist.

 class Player:
    def __init__(self, name):
        self.name = name
        self.score = 0
        self.vote = 0
        self.player_hand = []
        self.choice = ''
        self.player_hand = []
            
    def player_turn(self):
        print(self.name, "'s turn")


    def p_vote(self):
        print(self.name, " voted")

Now, we need to iterate through the list.

players_list = ['Ani', 'Paty', 'Felix', 'Alex']
players = [Player(player) for player in players_list]

Comments

0

Sounds like you're trying to dynamically create variables - write code that writes code.

You could try to use the exec built-in function.

players = ['Ani', 'Paty', 'Felix', 'Alex']

class Player:
    def __init__(self, name):
        self.name = name

    def p_vote(self):
        print(self.name + " voted.")

for player in players:
    exec( "%s = Player( '%s' )" %(player, player) )

Ani.p_vote()

Although, general internet advice has two points to make:

  • Be cautious where you use exec.
  • The Pythonic way is to write out the variables, "explicit is better than implicit."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.