Search code examples
pythontypeerror

python type error: does not support indexing


        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)

this a part of the code i ran in codeskulptor its showing an error: Line 73: TypeError: 'NoneType' does not support indexing ................................................................................................................................................................


Solution

  • list.remove works inplace and returns None. Thus line 74 sets alist to None:

                alist=alist.remove(alist[r])
    

    Just leave the assignment out.