现在没有缩进错误了:
def best_wild_hand(hand):
#Try all values for jokers in all 5-card selections.
blackJoker= "?B"
redJoker = "?R"
dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
listofLists = []
if blackJoker in hand:
newHand = hand.remove(blackJoker)
for d in dictSuit:
listofLists.append(newHand.append(d + "S"))
return listofLists
我正在尝试获取一个列表列表,其中是否在传递给 best_wild_hand 方法的手形参数列表中找到 blackJoker。如果发现黑手,我们将其移除并将 2..13 + C(牌组中发现的所有三叶草牌)附加到手上。我正在尝试制作一个包含 1 个三叶草的手牌列表(因此手牌列表 + nC)n 为数字 2- 13
我的预期输出是列表列表,每个列表中都有 2C...13C,它取代了黑色扑克
没有更多错误,但是当我在此打印语句中运行时,代码不会返回任何错误
print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])
最佳答案
进行编辑以匹配您的编辑
您的问题是您正在将变量分配给方法。这导致变量为 None
:
>>> y = ['6C'].remove('6C')
>>> print y
None
>>>
相反,改变
newHand = hand.remove(blackJoker)
至
newHand = hand
newHand.remove(blackJoker)
因此:
def best_wild_hand(hand):
#Try all values for jokers in all 5-card selections.
blackJoker= "?B"
redJoker = "?R"
dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
listofLists = []
if blackJoker in hand:
newHand = hand
newHand.remove(blackJoker)
for d in dictSuit:
listofLists.append(newHand.append(d + "S"))
return listofLists
现在当我运行你的代码时:
bash-3.2$ python safd.py
[None, None, None, None, None, None, None, None, None, None, None, None, None]
bash-3.2$
也许不是您想要的,但它仍然在打印
关于python - 列表列表,我似乎无法正确缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183269/