python类型错误:不支持索引

时间:2015-06-06 06:30:54

标签: python typeerror

        import simplegui
        import random        
        top=52
        in_play=False
        suits=('C','S','H','D')
        ranks=('A','2','3','4','5','6','7','8','9','T','J','Q','K')
        alist=range(0,52)

        def deal():
            global outcome,in_play,alist,top
            if not in_play:
                alist=range(0,52)
            in_play=True
            r = random.randint(0,top)
            card=Card(suits[alist[r]//13],ranks[alist[r]//4])  #line 73
            alist=alist.remove(alist[r])
            top=top-1
            return card
    class Card:
        def __init__(self,suit,rank):
            global suits,ranks
            if (suit in suits) and (rank in ranks):
                self.suit=suit
                self.rank=rank
            else:
                self.suite=None
                self.rank=None
                print "invalid card:",suit,rank

        def  __str__(self):
            return self.suit+self.rank
        def get_suit(self):
            return self.suit
        def get_rank(self):
            return self.rank
        def draw(self,canvas,pos):
            card_loc=(card_centre[0]+card_size[0]*ranks.index(self.rank),
                      card_centre[1]+card_size[1]*suits.index(self.suit) )
            canvas.draw_image(card_images,card_loc,card_size,[pos[0]+card_centre[0],pos[1]+card_centre[1]],card_size)

这是我在codeskulptor中运行的代码的一部分 它显示错误: 第73行:TypeError:'NoneType'不支持索引 .................................................. .................................................. .................................................. ..........

1 个答案:

答案 0 :(得分:0)

list.remove在原地工作并返回None。因此,第74行将alist设置为None

            alist=alist.remove(alist[r])

离开作业。