Python Forum
why my method doesn't find my List in the same class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why my method doesn't find my List in the same class?
#1
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
Reply
#2
Value is a class attribute. You need to access that with self, just as you do with the instance attribute cards.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 6 820 Apr-14-2025, 07:02 AM
Last Post: buran
  comtypes: how to provinde a list of string to a COM method zalanthas 0 939 Jun-26-2024, 01:27 PM
Last Post: zalanthas
  Extending list doesn't work as expected mmhmjanssen 2 1,381 May-09-2024, 05:39 PM
Last Post: Pedroski55
  class definition and problem with a method HerrAyas 2 1,479 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,434 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Why doesn't list require global keyword? johnywhy 9 3,967 Jan-15-2024, 11:47 PM
Last Post: sgrey
  How to read module/class from list of strings? popular_dog 1 1,324 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 2,472 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,122 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Using one child class method in another child class garynewport 5 3,209 Jan-11-2023, 06:07 PM
Last Post: garynewport

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020