0

嘿伙计们,我有一个关于测试我的方法的快速问题。上周我是 python 新手,但我正在尝试。我的任务是制作一种方法,该方法可以获取卡片列表并以某种方式对它们进行排序。我知道我所有的检查和 if 语句和这样的工作,因为我在 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()

因为我刚刚学了很多python idk,但我只是不确定我将如何使用那个测试“类”,我猜它被称为。

有任何想法吗?

非常感谢!

4

1 回答 1

2

似乎该play功能应该在solitaire正在导入的模块中。这意味着分配给变量的值actual

 actual = solitaire.play()

是存储在函数pile变量中的结果play。然后,测试函数将预期结果存储在变量 中expected。因此,您应该将solitaire模块和测试脚本放在同一目录中,然后运行:

 python test.py

它应该会给你一些反馈。运行你所拥有的会产生一些错误。通过解释器运行play函数应该会给你一些关于哪里有错误的反馈。

编辑:

在OP的评论后编辑。

pile你在play函数中调用什么并不重要。它是由函数返回的,所以当你做类似actual = solitaire.play()的事情时,它会返回堆并将其分配给实际。

退一步说,你有两段代码:play函数和类Test。代码的设置方式应该在两个单独的文件中,我们称它们为 solitaire.py 和 test.py。play函数应该在 solitaire.py 文件中,而类Test应该在 test.py 中。test.py 脚本正在从 solitaire.py 文件中导入函数,其中包含以下行

import solitaire

这允许你做类似的事情。

actual = solitaire.play()

这一行基本上相当于说“从纸牌模块使用播放功能并将它返回的数据分配给变量实际。” 要运行脚本,请确保纸牌和测试文件位于同一目录中,并且该目录是终端中的工作目录。然后,您只需进入python test.py终端并让它运行。

如果您在理解 Python、如何运行脚本等方面需要进一步的帮助,请查看诸如Learn Python the Hard Way 之类的内容。

于 2012-11-09T04:19:54.413 回答