好的,这里有一些背景信息:
我的纸牌游戏有 4 个玩家,每个玩家都有一手牌。pHands 是其他 4 个玩家手牌的列表(pHands 中还有 4 个其他列表)
列表在 pHands(玩家手中)中看起来像这样:['as', '2s', '4h', .... , 'ad']
列表中每个元素的第一个字符是卡片,列表中每个元素的第二个字符是套件。
我想在列表的每个元素中取出西装,所以我有以下功能:
def slicing(player):
slicing_p1(player)
slicing_p2(player)
def slicing_p1(player):
pHandsSlice = pHands[player]
pHandsString = ", ".join(pHands[player])
x = len(pHands[player])
for i in range(x):
y = ''.join(pHandsSlice[i])
y = y.replace(y[1], "")
global myStrList
global myStr
myStrList = myStrList + y
myStr = myStr + y + ","
def slicing_p2(player):
x = len(myStr)
global myStr
global myStrList
myStr = myStr[:-1]
myStrList = list(myStrList)
然后我执行这些功能:
slicing(0)
slicing(1) <------- this is where the error occurs.
错误:
File "C:\Users\xxx\Downloads\UPDATE Assignment 2 (of 2)\GoFishPack\GoFishGameEngineSkeleton.py", line 63, in slicing
slicing_p1(player)
File "C:\Users\xxx\Downloads\UPDATE Assignment 2 (of 2)\GoFishPack\GoFishGameEngineSkeleton.py", line 75, in slicing_p1
myStrList = myStrList + y
TypeError:只能将列表(不是“str”)连接到列表
这是怎么回事,我该如何解决?