【问题标题】:Try to make a function that will identify players and add two players' cards to left or right side尝试制作一个可以识别玩家并将两个玩家的卡片添加到左侧或右侧的功能
【发布时间】:2018-11-19 04:59:57
【问题描述】:

给出了一个空列表,我正在尝试创建一个函数来识别玩家,以便将 player1 的卡添加到空列表的左侧,并将 player2 的卡添加到空列表的右侧。 现在我有这个功能:

class OnTable:

    def __init__(self):
        self.__cards = []

    def place(self,player,card):

        if player == 'player2':
            self.__cards.append(card)

        elif player == 'player1':
            self.__cards.insert(0,card)

    def __str__(self):

        list1 = '['
        for item in self.__cards:
            list1 += (str(item)+' ')

        list1 = re.sub(' ', ' ', list1.strip())

        return list1 + ']'   

但是,在我使用以下命令运行此函数后:

table = Ontable()
table.place(player1,card)

如果只给我一个空列表...里面什么都没有,有没有办法让函数知道谁是玩家? 谢谢!

【问题讨论】:

    标签: python-3.x list insert append


    【解决方案1】:

    table.place(player1,card) 应改为table.place('player1','Ace of Hearts')。根据您的位置功能,播放器 1 应该是一个字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多