4 Replies - 650 Views - Last Post: 04 September 2013 - 11:24 AM Rate Topic: -----

#1 cickozg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 13

How do I print content of list object ?

Posted 04 September 2013 - 10:20 AM

I need help with printing list of cards in player hand. This is my __str__ method for card and hand class

class Card:
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
        
    def __str__(self):
        return str(self.suit) + str(self.rank)


class Hand:
    def __str__(self):
        self.hand_str = ""
        for c in self.card_list:
            self.hand_str += str(c)+ " "
        return self.hand_str


Output I get is : U ruci 1. su vam: <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000001D6B648>> <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000001D6B648>> <bound method Deck.draw_card of <__main__.Deck instance at 0x0000000001D6B648>>


Second problem is when i choose card i want to throw i get an error: thrown_cards.extend(igrac.card_list[int(x)-1])
TypeError: 'instancemethod' object is not iterable

Code for printing a list of cards and chosing a card is:

            if self.current_player == 0:
                print "U ruci 1. su vam: ",igrac
                x = input("Koju kartu zelite baciti (redni broj) ?")
                thrown_cards.extend(igrac.card_list[int(x)-1])
                self.next_turn()


Please help

Is This A Good Question/Topic? 0
  • +

Replies To: How do I print content of list object ?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: How do I print content of list object ?

Posted 04 September 2013 - 10:56 AM

Here is a simple example I built using your classes. I added a few methods that I think you have, but just didn't show.

class Card:
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
        
    def __str__(self):
        return str(self.suit) + str(self.rank)


class Hand:
	def __init__(self):
		self.card_list = []

	def addCard(self,card):
		self.card_list.append(card)

	def __str__(self):
		self.hand_str = ""
		for c in self.card_list:
			self.hand_str += str(c)+ " "
		return self.hand_str

h = Hand()

c1 = Card('d','2')
c2 = Card('c','4')
c3 = Card('h','A')

h.addCard(c1)
h.addCard(c2)
h.addCard(c3)

print(h)



Hope this helps you get the idea. :)
Was This Post Helpful? 1
  • +
  • -

#3 cickozg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 13

Re: How do I print content of list object ?

Posted 04 September 2013 - 11:09 AM

I have all of that, but dont know why I dont get output like that...

Here, try to compile it. And what about the error?

# -*- 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 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_card[-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_card[-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,x
        while True:
            if self.current_player == 0:
                print "U ruci 1. su vam: "
                print(igrac)
                x = input("Koju kartu zelite baciti (redni broj) ?")
                thrown_cards.extend(igrac.card_list[x-1])
                del igrac.card_list[x-1]
                self.next_turn()
            elif self.current_player == 1:
                igrac1.izaberi_kartu()
                igrac1.baci_kartu()
                self.next_turn()
            elif self.current_player == 2:
                igrac2.izaberi_kartu()
                igrac2.baci_kartu()
                self.next_turn()
            elif self.current_player == 3:
                igrac3.izaberi_kartu()
                igrac3.baci_kartu()
                self.next_turn()



kajla = Game()
kajla.play()

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: How do I print content of list object ?

Posted 04 September 2013 - 11:16 AM

What is the error? We are not mind readers you know. Nor do we just go compiling any code that someone gives us. ;)
Was This Post Helpful? 0
  • +
  • -

#5 cickozg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 13

Re: How do I print content of list object ?

Posted 04 September 2013 - 11:24 AM

View PostMartyr2, on 04 September 2013 - 11:16 AM, said:

What is the error? We are not mind readers you know. Nor do we just go compiling any code that someone gives us. ;)/>

It's written on first post :)

thrown_cards.extend(igrac.card_list[int(x)-1])
TypeError: 'instancemethod' object is not iterable
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1