#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Lightbulb 'instancemethod' object is not iterable


    Hello, I'm working on card game, for now textual version. I'm having problem with chosing a card to throw. Error I get is:

    Traceback (most recent call last):
    File "C:\Users\Petar\Desktop\aaaa.py", line 187, in <module>
    kajla.play()
    File "C:\Users\Petar\Desktop\aaaa.py", line 164, in play
    thrown_cards.extend(igrac.card_list[x-1])
    TypeError: 'instancemethod' object is not iterable

    I'm choosing card with:

    Code:
    x = input("Koju kartu zelite baciti (redni broj) ?")
                    thrown_cards.extend(igrac.card_list[x-1])
    I'll put whole code if it is needed
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    522
    Rep Power
    34
    thrown_cards.extend(igrac.card_list[x-1])
    TypeError: 'instancemethod' object is not iterable
    It says that igrac.card_list is a class instance, not a list. You perhaps define it in more than one place. We can't tell anything more without code.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,960
    Rep Power
    481
    You're using python 2.
    The extend method, assuming thrown_cards is a list, expects an iterable argument.

    The short answer is that for you to instead use
    thrown_cards.append

    But you should make sure that igrac.card_list[x-1] is what you think it ought to be. For diagnosis use
    Code:
                    x = input("Koju kartu zelite baciti (redni broj) ?")
                    print('str(x) and str(type(x)) are '+str(x)+'    '+str(type(x)))
                    thrown_cards.append(igrac.card_list[x-1])
    run your program and examine the new output.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by dwblas
    It says that igrac.card_list is a class instance, not a list. You perhaps define it in more than one place. We can't tell anything more without code.
    Here is the code. I changed thrown_cards.extend to thrown_cards.append as b49P23TIvg told me, but now i get other error:

    File "C:\Users\Petar\Desktop\aaaa.py", line 61, in izaberi_kartu
    if VALUE[c.rank] >= VALUE[thrown_cards[-1].rank] or c.rank == "2":
    AttributeError: 'function' object has no attribute 'rank'

    Here is the code, I'm desperate with it...

    Code:
    # -*- coding: cp1250 -*-
    import random
    
    #Karte
    SUITS = ['P', 'T', 'H', 'K']
    RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    VALUE = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'J':10, 'Q':11, 'K':12, 'A':13, '10':14}
    thrown_cards = []
    
    
    class Card:
        def __init__(self,suit,rank):
            self.suit = suit
            self.rank = rank
            
        def __str__(self):
            return str(self.suit) + str(self.rank)
    
    class Deck:
        def __init__(self):
            self.deck_list = []
            global SUITS, RANKS
            for s in SUITS:
                for r in RANKS:
                    self.deck_list.append(Card(s,r))
    
        def shuffle(self):
            return random.shuffle(self.deck_list)
    
        def draw_card(self):
            return self.deck_list.pop()
    
    class Hand:
        def __init__(self):
            self.card_list = []
            self.card_list2 = []
            self.card_list3 = []
    
        def __str__(self):
            self.hand_str = ""
            for c in self.card_list:
                self.hand_str += str(c)+ " "
            return self.hand_str
        
        def add_card(self,card):
            self.card_list.append(card)
        def add_card2(self,card):
            self.card_list2.append(card)
        def add_card3(self,card):
            self.card_list3.append(card)
    
        def vuci_kartu(self):
            if len(deck.deck_list) > 0 and len(self.card_list) < 3:
                self.card_list.append(deck.draw_card)
    
        def izaberi_kartu(self):
            self.choices = []
            if len(thrown_cards) > 0:
                if len(self.card_list) > 0:
                    for c in self.card_list:
                        if VALUE[c.rank] >= VALUE[thrown_cards[-1].rank] or c.rank == "2":
                            self.choices.extend(c)
                elif len(self.card_list) == 0 and len(self.card_list2) > 0:
                    for c in self.card_list2:
                        if VALUE[c.rank] >= VALUE[thrown_cards[-1].rank] or c.rank == "2":
                            self.choices.extend(c)
                else:
                    self.choices.extend(random.choice(self.card_list3))
                self.choices.sort()
            else:
                if len(self.card_list) > 0:
                    self.card_list.sort()
                    self.choices.extend(self.card_list[0])
                elif len(self.card_list) == 0 and len(self.card_list2) > 0:
                    self.card_list2.sort()
                    self.choices.extend(self.card_list2[0])
                else:
                    self.choices.extend(random.choice(self.card_list3))
    
        def baci_kartu(self):
            global thrown_cards
            if len(self.choices) > 0:
                self.choices.sort()
                thrown_card.extend(self.choices[0])
                if len(self.card_list) > 0:
                    del self.card_list[self.choices.index(self.choices[0])]
                elif len(self.card_list) == 0 and len(self.card_list2) > 0:
                    del self.card_list2[self.choices.index(self.choices[0])]
                else:
                    if len(thrown_cards) > 0:
                        if VALUE[self.choices[0].rank] < VALUE[thrown_cards[-1].rank] and VALUE[self.choices[0].rank] != "2":
                            del self.card_list3[index(self.choices[0])]
                            self.card_list.extend(thrown_cards)
                            thrown_cards = []
                        else:
                            del self.card_list3[self.choices.index(self.choices[0])]
            else:
                self.card_list.extend(thrown_cards)
                thrown_cards = []
    
      
    class Game:
        def __init__(self):
            global deck,igrac,igrac1,igrac2,igrac3
            deck = Deck()
            self.count = 4
            self.current_player = 0
            igrac = Hand()
            igrac.add_card(deck.draw_card)
            igrac.add_card(deck.draw_card)
            igrac.add_card(deck.draw_card)
            igrac.add_card2(deck.draw_card)
            igrac.add_card2(deck.draw_card)
            igrac.add_card2(deck.draw_card)
            igrac.add_card3(deck.draw_card)
            igrac.add_card3(deck.draw_card)
            igrac.add_card3(deck.draw_card)
    
            igrac1 = Hand()
            igrac1.add_card(deck.draw_card)
            igrac1.add_card(deck.draw_card)
            igrac1.add_card(deck.draw_card)
            igrac1.add_card2(deck.draw_card)
            igrac1.add_card2(deck.draw_card)
            igrac1.add_card2(deck.draw_card)
            igrac1.add_card3(deck.draw_card)
            igrac1.add_card3(deck.draw_card)
            igrac1.add_card3(deck.draw_card)
    
            igrac2 = Hand()
            igrac2.add_card(deck.draw_card)
            igrac2.add_card(deck.draw_card)
            igrac2.add_card(deck.draw_card)
            igrac2.add_card2(deck.draw_card)
            igrac2.add_card2(deck.draw_card)
            igrac2.add_card2(deck.draw_card)
            igrac2.add_card3(deck.draw_card)
            igrac2.add_card3(deck.draw_card)
            igrac2.add_card3(deck.draw_card)
    
            igrac3 = Hand()
            igrac3.add_card(deck.draw_card)
            igrac3.add_card(deck.draw_card)
            igrac3.add_card(deck.draw_card)
            igrac3.add_card2(deck.draw_card)
            igrac3.add_card2(deck.draw_card)
            igrac3.add_card2(deck.draw_card)
            igrac3.add_card3(deck.draw_card)
            igrac3.add_card3(deck.draw_card)
            igrac3.add_card3(deck.draw_card)
            
    
        def next_turn(self):
            self.current_player += 1
            self.current_player %= self.count
    
        def play(self):
            global thrown_cards
            while True:
                if self.current_player == 0:
                    print "U ruci 1. su vam: "
                    print(igrac)
                    x = input("Koju kartu zelite baciti (redni broj) ?")
                    print('str(x) and str(type(x)) are '+str(x)+'    '+str(type(x)))
                    thrown_cards.append(igrac.card_list[x-1])
                    del igrac.card_list[x-1]
                    igrac.vuci_kartu()
                    self.next_turn()
                elif self.current_player == 1:
                    igrac1.izaberi_kartu()
                    igrac1.baci_kartu()
                    vuci_kartu()
                    self.next_turn()
                elif self.current_player == 2:
                    igrac2.izaberi_kartu()
                    igrac2.baci_kartu()
                    vuci_kartu()
                    self.next_turn()
                elif self.current_player == 3:
                    igrac3.izaberi_kartu()
                    igrac3.baci_kartu()
                    vuci_kartu()
                    self.next_turn()
    
    
    
    kajla = Game()
    kajla.play()

    Other problem I'm facing is the output of print(igrac). I'm getting like this:
    <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000002D79908>> <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000002D79908>> <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000002D79908>>

    Dont know why, __str__ methods are looking fine to me
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,960
    Rep Power
    481
    Where you have
    deck.draw_card
    You should have
    deck.draw_card()
    In other words, invoke the method.

    I wrote a deal function for you and changed the Game.__init__ method. That's not too important; calling the method with deck.draw_card() is important.

    Untested. I'm on a Windows system. Microsoft and I have fundamental philosophical differences. Probably the biggest issue would be that Windows goes out of its way to hide and obstruct paths. Whereas I like to know where my files are. And then yesterday I swapped offices going from 2 to 1 monitor but several programs continued to display on the non-existent monitor. I had to unswap offices, and today I'll have to reboot.
    Code:
    def deal(deck):
        igrac = Hand()
        for f in (igrac.add_card, igrac.add_card2, igrac.add_card3):
    	for i in 'abc':
    	    f(deck.draw_card())
        return igrac
    
    class Game:
        def __init__(self):
            global deck,igrac,igrac1,igrac2,igrac3
            deck = Deck()
            self.count = 4
            self.current_player = 0
            (igrac, igrac1, igrac2, igrac3) = (deal(deck) for i in 'abcd')
    I see other extend methods that should probably be append, the Deck class should support the len function (that is, have a __len__ method) so you don't need to know details of the Deck object outside of the Deck class,
    Code:
        def vuci_kartu(self):
            if len(deck.deck_list)

    Comments on this post

    • cickozg agrees : Solution to my problem
    Last edited by b49P23TIvg; September 5th, 2013 at 09:28 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Looks like deck.draw_card() was doing whole problem, I just added () and now program is working fine. Thank you very much

    Will play with code you wrote. Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo