Python 列表列表和我可以';我好像没把缩进的地方弄对
现在没有缩进错误:Python 列表列表和我可以';我好像没把缩进的地方弄对,python,indentation,Python,Indentation,现在没有缩进错误: 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 } listofL
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语句中运行时,代码不会返回任何错误
print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])
编辑以匹配您的编辑
您的问题是将变量分配给方法。这导致变量为None
:
>>> y = ['6C'].remove('6C')
>>> print y
None
>>>
取而代之的是改变
newHand = hand.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$
也许不是你想要的,但它正在打印你的输出是什么?你能分享一下吗?我没有得到输出,我只是把你的函数粘贴了进去。你是Rihgh,我只是不明白为什么print语句没有返回任何它应该返回列表的内容。hanks@A.J我想我必须使用list()函数来真正形成一个列表,但是的,这是可以解决的@紫色薄雾,如果这让你晕眩,你介意点击我答案旁边的绿色复选框(接受它),你也会得到声誉!使用任何强制缩进的编辑器或IDE。让你的问题消失。
bash-3.2$ python safd.py
[None, None, None, None, None, None, None, None, None, None, None, None, None]
bash-3.2$