Hi!

I've created three list in a class but when I print them i get <__main__.Kort instance at 0x16eb22b0>

Anyone know how I can get the list to print put its "real" elements?

class Kortlek():
    def __init__(self,colors=["blue   ", "yellow ", "red    "], forms=["triangel", "circle  ", "square  "], numbers=[1, 2, 3], grades=[1, 2, 3]):
        self.cardList = []
        self.playerCardList = []
        self.lista3 = []
        for color in colors:
            for form in forms:
                for number in numbers:
                    for grade in grades:
                        tmp= Kort(color, form, number, grade)
                        self.cardList.append(tmp)

I read somewhere that you should use __str__ but I didn't understand how...

Thanks! :)

Recommended Answers

All 6 Replies

You must add a member function __str__ to the class, which returns a string representing the instance, for example

class Kort(object):
    ...
    def __str__(self):
         return "Kort({s},{f},{n},{g})".format(
             s = self.color, f = self.form, n = self.number, g = self.grade)

Example of input (instance of the class) and expected output would help.

I get error:

Traceback (most recent call last):
  File "J:/Python26/kort.py", line 13, in <module>
    kort = Kortlek()
  File "J:/Python26/kort.py", line 10, in __init__
    tmp= Kort(color, form, number, grade)
NameError: global name 'Kort' is not defined
>>>

Type in this and run kortutdelning()

:)

class Kortlek():
    def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
        self.cardList = []
        self.playerCardList = []
        self.lista3 = []
        for color in colors:
            for form in forms:
                for number in numbers:
                    for grade in grades:
                        tmp= Kort(color, form, number, grade)
                        self.cardList.append(tmp)

    def __str__(self):
        self.cardList
        self.lista3
        self.playerCardList


    def spelkorten(self):
        for a in random.sample(self.cardList, 6):
            self.playerCardList.append(a)

        self.skrivUt()

        self.fraga()
        

    def fraga(self):

        val2 = "5"
        while val2 !="2" or "1":
            
        
            val2 = raw_input("Vill du ta bort tre korta blalba??\n1.Ja\n2.Avsluta\n")
        
            if val2 == "1":
                for kort in range(3):
                    txt = "Skriv in nummret på kort " + str(kort) + ":\n"
                    kortID = int(raw_input(txt))
                    try:
                        self.lista3.append(self.playerCardList.pop(kortID-1))
                        print "-" * 50
                        [i for i in self.cardList if i not in self.lista3]
                        print self.lista3
                    
                
                    except IndexError:
                        print "Det angivna kortet finns inte i din hög!"
                        kortID = int(raw_input(txt))
                        self.lista3.append(self.playerCardList.pop(kortID-1))
                    
                self.laggTill()
         

            elif val2 == "2":
                quit()

            else:
                print "Välj aningen 1 eller 2"

            

    def laggTill(self):
        for a in random.sample(self.cardList, 3):
            self.playerCardList.append(a)

        self.skrivUt()
        self.fraga()

    def skrivUt(self):
        for b in self.playerCardList:
            b.visa()
            

    def kortjamforelse(self):
        self.lista3
        self.fraga()
        self.skrivUt()
        


            
            

class Kort():
    def __init__(self,color,form,number,grade):
        self.color = color
        self.form = form
        self.number = number
        self.grade = grade
    def visa(self):
        text = str(self.color) + "\t"
        text += str(self.form) + "\t"
        text += str(self.number) + "\t"
        text += str(self.grade)
        print text

def kortutdelning():
    kort = Kortlek(["blue   ", "yellow ", "red    "],["triangel", "circle  ", "square  "],[1, 2, 3],[1, 2, 3])
    kort.spelkorten()

The only list I would like to get in that way is lista3 :)

Thanks

You can do that in two ways, the second way is usually preferred because everything is contained in the class:

import random

class Kortlek():
    def __init__(self,colors=["blue   ", "yellow ", "red    "], \
                 forms=["triangel", "circle  ", "square  "], \
                 numbers=[1, 2, 3], grades=[1, 2, 3]):
        self.cardList = []
        self.playerCardList = []
        self.lista3 = []

        ## add something to lista3
        for x in range(5):
            self.lista3.append(random.randint(1, 10))

    def print_list(self):
        print self.lista3

KC = Kortlek()
print KC.lista3
print
KC.print_list()

Or

def __str__(self):
        return str(self.lista3)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.