Python Forum

Full Version: why my method doesn't find my List in the same class?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Value is a class attribute. You need to access that with self, just as you do with the instance attribute cards.
(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 :)