Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

debugging - type object 'Player' has no attribute 'name' in python card game

UI.print_message('{} draws {} cards.'.format(player.name, picked))

AttributeError: type object 'Player' has no attribute 'name'

Currently debugging a card game for a project and can't figure out how to fix this error. Can't make sense of why it's saying there's no attribute 'name' as it is clearly defined in the constructor. Any help would be appreciated!

Code for reference:

This line defines the players :

player_info = UI.get_player_information(MAX_PLAYERS)
self.players = [player_classes(name) for name, typ in player_info]

and then run_player is called :

i = 0 # current player index
    while True:
        # process current player's turn
        won = self.run_player(self.players[i])


 def run_player(self, player):

    if self.skip:
        # return without performing any discard
        self.skip = False
        UI.print_message('{} is skipped.'.format(player.name)) #<=== ERROR
        return False
    if self.draw2:
        # draw two cards
        picked = self.pick_up_card(player, 2)
        self.draw2 = False
        print(player.name)
        UI.print_message('{} draws {} cards.'.format(player.name, picked)) #<=== ERROR
    if self.draw4:
        # draw four cards
        picked = self.pick_up_card(player, 4)
        self.draw4 = False
        UI.print_message('{} draws {} cards.'.format(player.name, picked)) #<=== ERROR

    top_card = self.discards[-1]
    hand_sizes = len([p.hand for p in self.players])
    UI.print_player_info(player, top_card, hand_sizes)

class Player:

    is_ai = False
    hand = []

    def __init__(self, name):
        self.name = name
        self.hand = []

    @staticmethod
    def select_card(choices, _):
        """Select a card to be discarded

        Delegates choice to user interface.
        """
        return UI.select_card(choices)

    @staticmethod
    def ask_for_swap(others):
        """Select a player to switch hands with

        Delegates choice to user interface.
        """
        return UI.select_player(others)

Code for get_player_information :

def get_player_information(max_players):
   """get required information to set up a round"""

   # create players list
   player_info = []
   # how many human players?
   print("
How many human players [1-4]:")
   no_of_players = get_int_input(1, max_players)

   # for each player, get name
   for i in range(no_of_players):
       print(f"Please enter the name of player {i+1}:")
       player_info.append(('human', get_string_input()))

   ai_names = ['Angela', 'Bart', 'Charly', 'Dorothy']

   # how many AI players? ensure there are at least 2 players
   min_val = 1 if (len(player_info) == 0) else 0
   max_val = max_players - no_of_players
   print(f"
How many ai players [{min_val:d}-{max_val:d}]:")
   no_of_players = get_int_input(min_val, max_val)

   # randomly assign a simple or smart AI for each computer strategy
   for name in ai_names[:no_of_players]:
       if [True, False]:
           player_info.append(('simple', name))
       else:
           player_info.append(('smart', f"Smart {name}"))

   return player_info
question from:https://stackoverflow.com/questions/65830160/type-object-player-has-no-attribute-name-in-python-card-game

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...