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
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Why doesn't list require global keyword? johnywhy 9 654 Jan-15-2024, 11:47 PM
Last Post: sgrey
  How to read module/class from list of strings? popular_dog 1 425 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 1,000 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,049 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Using one child class method in another child class garynewport 5 1,490 Jan-11-2023, 06:07 PM
Last Post: garynewport
  Find (each) element from a list in a file tester_V 3 1,157 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,372 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,907 May-31-2022, 01:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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