尝试将字符串与某些列表缺陷的值连接起来

时间:2018-02-20 18:32:14

标签: python python-2.7

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

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__'

现在即使我能理解错误抛出的背景,我甚至还没有开始研究模块和__init__():样式的python,因此我无法来接近理解这个问题。如果有人知道我能从这里做什么,我真的很感激任何帮助。谢谢!

修改

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

最后,我为这篇文章的不一致性质道歉,如果我能做更多的事情来否定下来的选票,请告诉我。谢谢。

2 个答案:

答案 0 :(得分:2)

你得到的错误是

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

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

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

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

在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。

答案 1 :(得分:0)

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

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