现在没有缩进错误:
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方法的hand参数列表中找到blackJoker。如果发现黑手,我们将其移除并将2..13 + C(在牌组中找到的所有三叶草卡片)附加到手上。我正在尝试制作包含1个三叶草的手牌列表(所以手牌列表+ nC)n为数字2 - 13
我的预期输出是每个列表中有2C ... 13C的列表列表,它取代了黑色扑克
没有更多错误但是当我在这个打印语句中运行代码时,不会返回任何错误
print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])
答案 0 :(得分:1)
您的问题是您正在为方法分配变量。这导致变量为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$
也许不是你想要什么,但它仍在打印