嘿傢伙我有一個關於測試我的方法的快速問題。即時通訊新的蟒蛇截至上週,但即時通訊嘗試。我的任務是製作一種方法,以某種方式列出卡片並排序。我知道我所有的檢查和如果聲明和這樣的工作,因爲我測試了他們在python shell,但我想如果我把我所有的代碼到shell它將會出現索引(正確的縮進)。所以如果你們能夠幫助那些我只是不知道如何實現我的方法的東西。蟒蛇測試我的方法
這裏是我的方法:
def play(list):
for k in list:
a,x,z = 0
#insert into the pile
pile = []
pile = pile + [[list[k]]]
#check if the new card in the pile is compatible to the card 3 to the left
#check in terms if the first char of each card is compatible
if((((pile[a])[(len(pile[a]))-1])[0]) == ((((pile[a-3])[(len(pile[a-3]))-1])[0]))):
#add card to the top of the card 3 to left
pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
if (len(pile[a])) == 1:
del pile[a]
else:
del (pile[a])[-1]
#check in terms if the last char of each card is compatible
elif((((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-3])[(len(pile[a-3]))-1])[(len((((pile[a-3])[(len(pile[a-3]))-1]))))-1])):
#add card to the top of the card 3 to left
pile[a-3] = pile[a-3] + [((pile[a])[(len(pile[a]))-1])]
if (len(pile[a])) == 1:
del pile[a]
else:
del (pile[a])[-1]
#check if the new card in the pile is compatible to the card to the left
#check in terms if the first char of each card is compatible
elif((((pile[a])[(len(pile[a]))-1])[0]) == ((((pile[a-1])[(len(pile[a-1]))-1])[0]))):
pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
if (len(pile[a])) == 1:
del pile[a]
else:
del (pile[a])[-1]
#check in terms if the last char of each card is compatible
elif((((pile[a])[(len(pile[a]))-1])[(len((((pile[a])[(len(pile[a]))-1]))))-1]) == (((pile[a-1])[(len(pile[a-1]))-1])[(len((((pile[a-1])[(len(pile[a-1]))-1]))))-1])):
pile[a-1] = pile[a-1] + [((pile[a])[(len(pile[a]))-1])]
if (len(pile[a])) == 1:
del pile[a]
else:
del (pile[a])[-1]
else:
a = a + 1
#now go through and look for additional moves
#(if any match the 1 to the left or 3rd)
while x < (len(pile)):
if((((pile[x])[(len(pile[x]))-1])[0]) == ((((pile[x-3])[(len(pile[x-3]))-1])[0]))):
pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
if (len(pile[x])) == 1:
del (pile[x])[-1]
z = x + 1
while z < (len(pile)):
pile[z-1] = pile[z]
z = z + 1
del pile[(len(pile))-1]
else:
del (pile[x])[-1]
x = x + 1
elif((((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-3])[(len(pile[x-3]))-1])[(len((((pile[x-3])[(len(pile[x-3]))-1]))))-1])):
pile[x-3] = pile[x-3] + [((pile[x])[(len(pile[x]))-1])]
if (len(pile[a])) == 1:
del (pile[x])[-1]
z = x + 1
while z < (len(pile)):
pile[z-1] = pile[z]
z = z + 1
else:
del (pile[a])[-1]
x = x + 1
elif((((pile[x])[(len(pile[x]))-1])[0]) == ((((pile[x-1])[(len(pile[x-1]))-1])[0]))):
pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
if (len(pile[x])) == 1:
del (pile[x])[-1]
z = x + 1
while z < (len(pile)):
pile[z-1] = pile[z]
z = z + 1
del pile[(len(pile))-1]
else:
del (pile[x])[-1]
x = x + 1
elif((((pile[x])[(len(pile[x]))-1])[(len((((pile[x])[(len(pile[x]))-1]))))-1]) == (((pile[x-1])[(len(pile[x-1]))-1])[(len((((pile[x-1])[(len(pile[x-1]))-1]))))-1])):
pile[x-1] = pile[x-1] + [((pile[x])[(len(pile[x]))-1])]
if (len(pile[a])) == 1:
del (pile[x])[-1]
z = x + 1
while z < (len(pile)):
pile[z-1] = pile[z]
z = z + 1
else:
del (pile[x])[-1]
x = x + 1
else:
x = x + 1
#end while loop
#end of for loop
return pile
這就好比什麼我的老師給我,像測試用例:
import unittest
import solitaire
class Test(unittest.TestCase):
def test1(self):
actual = solitaire.play(['AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC',
'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
'AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS'])
expected = [['AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS','KH','QH','JH','TH','9H','8H','7H','6H','5H','4H','3H','2H','AH','AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD','KC','QC','JC','TC','9C','8C','7C','6C','5C','4C','3C','2C','AC']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
def test2(self):
actual = solitaire.play(['QS','5D','4S','8D','8H','3C','3H','5C','9H','6S','QD','2S','2C',
'KS','QC','7C','JC','4H','3D','5S','6C','KD','JS','9C','TS','2D',
'4D','AS','AC','7H','TC','AH','KH','6D','4C','8C','TD','AD','8S',
'3S','JH','KC','QH','TH','6H','7S','5H','2H','9S','7D','JD','9D'])
expected = [['8D','8H','3H','3C','JC','7C','QC','QD','4D','4S','6S','KS','KD','5D','5C','2C','2S','QS'],
['9C','4C','4H','9H'],
['6D','2D','3D'],
['JS','TS','TC','6C','AC','AS','5S'],
['KH','AH','7H'],
['8C'],
['AD','TD'],
['3S','8S'],
['6H','TH','JH'],
['KC'],
['2H','QH'],
['9D','JD','7D','7S'],
['5H'],
['9S']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
def test3(self):
actual = solitaire.play(['5S','9H','QH','2H','8H','6H','3S','3H','AS','5H','JH','3C','KH',
'4H','6S','8S','8D','2D','JD','AH','KC','TD','TC','3D','6D','2C',
'AD','QS','9C','7C','6C','QD','JS','7H','7S','7D','KD','TS','5D',
'KS','9S','9D','4C','5C','8C','QC','JC','TH','4S','4D','2S','AC'])
expected = [['4D','5D','5C','8C','QC','JC','4C','4S','TS','AS','AH','4H','5H','7H','7S','JS','QS','QD','AD','6D','3D','TD','JD','2D','8D','8S','6S','KS','KD','7D','9D','9C','7C','6C','2C','TC','3C','KC','KH','JH','9H','QH','2H','8H','6H','3H','3S','5S'],
['9S'],
['TH'],
['2S'],
['AC']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
def test4(self):
actual = solitaire.play(['7C','9S','QC','4H','9D','3S','AD','9C','8H','AC','2D','9H','AH',
'5D','6C','QD','3H','TC','QS','2S','8D','7D','QH','6H','4C','3C',
'JS','JD','7H','TS','4S','TD','5H','KD','8C','KS','JC','6D','2H',
'5C','3D','KH','8S','JH','TH','KC','2C','5S','AS','4D','7S','6S'])
expected = [['3S','9S','9D','AD','AC','7C'],
['QD','8D','7D','2D','5D','5H','8H','4H','9H','9C','QC'],
['AH'],
['TC','6C'],
['6H','3H'],
['JD','JS','2S','QS'],
['QH'],
['5C','JC','8C','3C','4C'],
['7H'],
['6D','TD','KD','KS','4S','TS'],
['2H'],
['3D'],
['TH','KH'],
['AS','5S','8S'],
['JH'],
['2C','KC'],
['4D'],
['6S','7S']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
def test5(self):
actual = solitaire.play(['6C','AC','7C','7D','KC','6S','9H','5C','JS','3S','TS','9D','JH',
'QD','2D','8H','QS','8D','8C','2S','TC','9C','9S','5H','4D','3C',
'JD','QC','AS','3H','TD','7H','KS','KH','AD','TH','KD','4C','2C',
'QH','3D','5D','7S','4S','6D','2H','AH','JC','8S','4H','5S','6H'])
expected = [['JC','QC','2C','2H','QH','AH','4H','6H','6D','3D','5D','JD','TD','TH','7H','7S','4S','4C','TC','3C','3H','5H','KH','KS','KD','8D','4D','AD','AS','9S','9C','KC','5C','8C','8H','9H','JH','JS','TS','3S','6S','QS','2S','2D','QD','9D','7D','7C','AC','6C'],
['5S','8S']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
def test6(self):
actual = solitaire.play(['8S','7C','AS','5H','7H','9D','8C','8H','3H','TC','AH','KH','JH',
'JC','AD','9C','AC','QS','6H','KC','KS','5C','TS','3C','9H','7D',
'3S','4D','TD','QD','5D','9S','3D','6S','6D','8D','JS','2S','4C',
'4S','5S','6C','2D','4H','7S','2H','KD','TH','JD','QH','QC','2C'])
expected = [['5S','7S','JS','2S','4S','4C','4H','TH','QH','QC','6C','9C','AC','KC','KS','QS','3S','3C','5C','5D','QD','7D','4D','TD','TS','9S','6S','6D','3D','8D','2D','2H','KH','6H','9H','9D','AD','AS','AH','3H','5H','JH','JC','8C','TC','7C','7H','8H','8S'],
['JD','KD'],
['2C']]
self.assertEqual (len(expected), len(actual), 'Incorrect result')
self.assertListEqual( expected, actual, 'Incorrect result')
pass
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
因爲我剛剛得知蟒蛇IDK的那麼多,但我只是不當然,我會如何使用這個測試'班',我想它被稱爲。
任何想法的?
非常感謝!
你做得很好!但試着澄清你的問題。我想你是問你的python文件是否正確(去試試看)以及如何運行測試(閱讀我發佈的文檔)。如果你不知道如何獲得一個Python文件運行的外觀[這裏](http://docs.python.org/2/tutorial/interpreter.html#executable-python-scripts),如果你想看看如何處理測試[這裏](http://docs.python.org/2/library/unittest.html)。 – sunnyrjuneja
使用'unittest'的一點是,它會告訴你你是否正確。你只需要將'play'函數保存在一個名爲'solitaire.py'的文件中(因爲測試程序試圖「導入紙牌」,所以你必須匹配那個名字),然後運行測試腳本。 (如果它被稱爲'solitairetest.py',然後在shell中鍵入'python solitairetest.py')。它會給你一個很好的打印輸出你通過了哪些測試,如果你失敗了,哪裏出錯了。 – abarnert
Het謝謝@abarnert。我將它保存爲solitaire.py。所以現在我在python shell中執行'python solitairetest.py'? – erp