【问题标题】:Trying to concatenate a string with values of certain list indecies尝试将字符串与某些列表索引的值连接起来
【发布时间】:2018-07-31 05:08:30
【问题描述】:

我正在开发一个“扑克风格”游戏的自我项目,我遇到了一个错误,我需要向玩家展示玩家的手牌,为此我将玩家以及他们的手存储在一个列表中,我试图通过编写以下内容来连接两者:

print ('%s hand is: %s' % str(players[0]), str(hands[0,2]))
                                    ^ # edit: I have realized I was pulling from an incorrect source, modifying threw a new error I will detail below.
# player[] being the list of players
# and hands[] being the list of values pulled to represent cards

我在接近这条线时遇到的错误是:

Traceback (most recent call last):
  File "C:/.../poker.py", line 78, in <module>
    poker()
  File "C:/.../poker.py", line 73, in poker
    print ('%s hand is: %s' % str(player[0]), str(hands[0,2]))
TypeError: 'int' object has no attribute '__getitem__'

现在,即使我可以理解错误引发的上下文,我什至还没有开始研究模块和 python 的__init__(): 样式,因此我无法理解这个问题。如果有人知道我可以从这里做什么,我将非常感谢任何帮助。谢谢!

编辑

As requested, I am adding in the contents of the players and hands lists:

players begins as an empty set, players = [], and is appended by the values that the user passes as names for input, players 1-x.

hands begins as an empty set, hands = [], and is appended by the different 'cards' that are delt. Here are the code for these:

# def number of players first
player_amt = int(raw_input('How many players?: '))
# initialize player number variable
player = 1
# create a list for players to be stored in
players = []
# loop logic: to pull player names and assign a player number
# for as many players as specified above! 
while player <= player_amt:
  # gather player names
  player_name = raw_input('Enter player number %s\'s name: ' % player)
  # adding players to the list
  players.append(player_name)
  # to see the list being appended
  # print players
  # incremental counter
  player += 1

# ---

cards = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
cards_delt = 0
hands = []
while cards_delt < (len(players) * 2):
  card_delt_value = randint(0, len(cards) - 1)
  card_delt = cards[card_delt_value]
  cards_delt += 1
  hands.append(str(card_delt))

After reading through multiple times, I have spotted a bug, coming from the alert from the error that I previously posted, and have notated at to what happened there. I was pulling from an incorrect source, and have fixed this. But, now, upon doing so I am greeted with a new error I will detail it here:

Traceback (most recent call last):
  File "C:\...\poker.py", line 78, in <module>
    poker()
  File "C:\...\poker.py", line 73, in poker
    print ('%s hand is: %s' % str(players[0]), str(hands[0,2]))
TypeError: not enough arguments for format string

最后,对于这篇文章的不一致性质,我深表歉意,如果我可以再做些什么来否定反对票,请告诉我。谢谢。

【问题讨论】:

  • 您需要与我们分享playerhands 变量的内容,以及您希望从中获得什么价值。看起来问题出在hands[0,2],但目前我不确定你想在那里实现什么
  • 实际上,他需要编辑整个问题的内容,他将 player 或 hands 声明为整数,Python 解释器试图从整数而不是列表中获取项目。
  • print(player[0])print(hands[0,2]) 打印什么?
  • @Nick 你在谷歌上搜索过你得到的错误“TypeError: 'int' object has no attribute 'getitem'”stackoverflow.com/questions/11194110/…
  • stackoverflow.com/questions/11146190/… 我访问了这个页面,阅读了关于为什么它只提取一个值,第一个值在模数之后传递给它的参数,这对我来说很有意义,然后我去了为了解决这个问题,遇到了另一个错误,我不确定它是否会让我按照以下方式做一些事情:print ('%s' % (example[0,2])),因为它会说我确信列表索引必须是整数,而不是元组。

标签: python python-2.7


【解决方案1】:

你得到的错误说

TypeError: 'int' object has no attribute '__getitem__'

'__getitem__' 是只有集合才有的方法,它允许选择集合内的一个项目,通常使用两个括号之间的索引。

这意味着您正在尝试将索引运算符 [] 应用于 int,而不是列表。所以这意味着“手”或“玩家”都不是列表。

检查 TypeError 是否也告诉您正在使用的对象的类型,它是 'int' object。 Int 或整数表示此对象是整数。

在 Python 中声明集合的一些很酷的方法如下:

my_list = [item0, item1, item2]
my_tuple = (some_item, other_item, yet_another_item)
my_dictionary = {'apple': 'Manzana', 'good': 'Bueno'} 

my_dictionary['good'] 将返回“Bueno”,my_list[1] 将返回 item1。

【讨论】:

    【解决方案2】:

    您的代码中出现了一些错误。我相信这应该能够做你想做的事-

    print ('%s hand is: %s' %(str(player[0]),str(hands[0][2])))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多