【发布时间】: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
最后,对于这篇文章的不一致性质,我深表歉意,如果我可以再做些什么来否定反对票,请告诉我。谢谢。
【问题讨论】:
-
您需要与我们分享
player和hands变量的内容,以及您希望从中获得什么价值。看起来问题出在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