Python Forum
why my method doesn't find my List in the same class? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: why my method doesn't find my List in the same class? (/thread-15793.html)



why my method doesn't find my List in the same class? - Scorpio - Jan-31-2019

hello everybody,

I am begginner in python. I wrote the code under and I don't understand why I have the error you can find under

from random import *

class CardGame (object):
    Color = ["heart", "diamond", "club", "spade"]
    Value = [2, 3, 4, 5, 6, 7, 8, 9, 10, "valet", "queen", "king", "ace"]

    def __init__(self):
        self.cards = []
        for A in Value :
            for B in Color :
                self.cards.append((A, B))

    def CardName (self, Card):
        return "{0} of {1}".format(Value[Card[0]], Color[Card[1]])

    def ShuffleGame (self):
        return shuffle(self.cards)

    def TakeCard (self):
        return self.cards.pop()
Error:
Traceback (most recent call last): File "/Users/nic/Desktop/python/Card_Game_2.py", line 22, in <module> Game = CardGame() File "/Users/nic/Desktop/python/Card_Game_2.py", line 9, in __init__ for A in Value : NameError: name 'Value' is not defined
Thanks for your help


RE: why my method doesn't find my List in the same class? - ichabod801 - Jan-31-2019

Value is a class attribute. You need to access that with self, just as you do with the instance attribute cards.


RE: why my method doesn't find my List in the same class? - Scorpio - Jan-31-2019

(Jan-31-2019, 05:15 PM)ichabod801 Wrote: Value is a class attribute. You need to access that with self, just as you do with the instance attribute cards.

thanks you so much, you save my brain and probably my laptop as well :)