Python 列表索引超出迭代范围错误

Python 列表索引超出迭代范围错误,python,list,python-3.5,Python,List,Python 3.5,这段代码在第一次运行时运行良好,但是当它进入第二次迭代时,出现一个错误,说“列表索引超出范围”,尽管它第一次对它感到满意。索引是否需要重置 while pile_counter < 3: pile_one= operator.itemgetter(0,3,6,9,12,15,18)(cards) pile_two= operator.itemgetter(1,4,7,10,13,16,19)(cards) pile_three= op

这段代码在第一次运行时运行良好,但是当它进入第二次迭代时,出现一个错误,说“列表索引超出范围”,尽管它第一次对它感到满意。索引是否需要重置

    while pile_counter < 3:
        pile_one= operator.itemgetter(0,3,6,9,12,15,18)(cards)
        pile_two= operator.itemgetter(1,4,7,10,13,16,19)(cards)
        pile_three= operator.itemgetter(2,5,8,11,14,17,20)(cards)
        print("pile one is", pile_one, "pile two is", pile_two, "pile three is", pile_three)
        user_pile= input("which pile is your card in? ")
        ##check one two or three
        if user_pile== "one":
            cards= [pile_two+ pile_one + pile_three]
        elif user_pile== "two":
            cards= [pile_one+ pile_two+pile_three]
        else:
            cards=[pile_one+ pile_three+ pile_two]
        print(cards)
        pile_counter= pile_counter+ 1


The full code runs like this:



    import random
    import operator
    def cards_random_shuffle():
            with open('cards.txt') as f: 
                for word in f:
                    cards = word.strip().split(":")
            random.shuffle(cards)
            return cards
    #print(cards_random_shuffle()) 
    cards= cards_random_shuffle()

    def cards_sort(cards):
        pile_counter= 0
        while pile_counter < 3:
            pile_one= operator.itemgetter(0,3,6,9,12,15,18)(cards)
            pile_two= operator.itemgetter(1,4,7,10,13,16,19)(cards)
            pile_three= operator.itemgetter(2,5,8,11,14,17,20)(cards)
            print("pile one is", pile_one, "pile two is", pile_two, "pile three 
is", pile_three)
            user_pile= input("which pile is your card in? ")

            if user_pile== "one":
                cards= [pile_two+ pile_one + pile_three]
            elif user_pile== "two":
                cards= [pile_one+ pile_two+pile_three]
            else:
                cards=[pile_one+ pile_three+ pile_two]
            print(cards)
            pile_counter= pile_counter+ 1
    print(cards_sort(cards))
桩号计数器<3时的

pile_one=运算符.itemgetter(0,3,6,9,12,15,18)(卡片)
pile_two=运算符itemgetter(1,4,7,10,13,16,19)(卡片)
pile_three=运算符.itemgetter(2,5,8,11,14,17,20)(卡片)
打印(“第一堆是”,第一堆是,“第二堆是”,第二堆是,“第三堆是”,第三堆)
user_pile=input(“您的卡在哪一堆?”)
##检查一两个或三个
如果用户_pile==“一”:
卡片=[第二堆+第一堆+第三堆]
elif user_pile==“两个”:
卡片=[第一堆+第二堆+第三堆]
其他:
卡片=[第一堆+第三堆+第二堆]
打印(卡片)
桩计数器=桩计数器+1
完整代码如下所示:
随机输入
进口经营者
def卡\u随机\u洗牌():
将open('cards.txt')作为f:
对于f中的单词:
cards=word.strip().split(“:”)
随机。洗牌(牌)
回程卡
#打印(卡片\u随机\u洗牌())
卡片=卡片\u随机\u洗牌()
def卡\u分类(卡):
桩号计数器=0
当桩号计数器<3时:
pile_one=运算符.itemgetter(0,3,6,9,12,15,18)(卡片)
pile_two=运算符itemgetter(1,4,7,10,13,16,19)(卡片)
pile_three=运算符.itemgetter(2,5,8,11,14,17,20)(卡片)
打印(“第一堆是”,第一堆,“第二堆是”,第二堆,“第三堆
是“三号桩”
user_pile=input(“您的卡在哪一堆?”)
如果用户_pile==“一”:
卡片=[第二堆+第一堆+第三堆]
elif user_pile==“两个”:
卡片=[第一堆+第二堆+第三堆]
其他:
卡片=[第一堆+第三堆+第二堆]
打印(卡片)
桩计数器=桩计数器+1
打印(卡片\分类(卡片))
文件“cards.txt”包含以下内容:


红桃王牌:红桃二:红桃三:红桃四:红桃五:红桃六:红桃七:红桃九:红桃十:红桃杰克:红桃皇后:红桃王:红桃王牌:梅花二:梅花三:梅花四:梅花五:梅花六:梅花七:梅花八[第一堆+第二堆+第三堆],您正在创建一个只有一个元素的新列表,即列表。因此,您将得到如下结果:

[[card1,card2,…]
而不是
[card1,card2,…]


你可以使用
cards=pile\u one+pile\u two+pile\u three
来做你想做的事。

在像
cards=[pile\u one+pile\u two+pile\u three]
这样的行中,你正在创建一个只包含一个元素的新列表,这是一个列表。因此你得到了这样的结果:

[[card1,card2,…]
而不是
[card1,card2,…]


您可以使用
cards=pile\u one+pile\u two+pile\u three
来做您想做的事情。

使用for循环时,此代码会更干净:
for pile\u计数器范围(4)
粘贴所有错误回溯,以便我们可以看到导致错误的行。还提供一个可复制的示例请提供一个最小的、完整的、可验证的示例()。提供的代码似乎不完整。使用范围(4)中的for循环:
for pile\u计数器,此代码将更加清晰
粘贴所有错误回溯,以便我们可以看到导致错误的行。同时提供一个可复制的示例请提供一个最小、完整、可验证的示例()。提供的代码似乎不完整。