954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie seeking coding help

Hey, how's it going? I decided to pick up Python for the heck of it and have been having some trouble creating this Card Deck class. All this class is supposed to do is initialize a deck, and then draw a card at total random:

import random
class CardDeck():
    suit = ["Spades","Clubs","Hearts","Diamonds"]
    number = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
       
    def DrawCard():
        randomsuit = random.randrange(0,3)
        randomnumber = random.randrange(0,12)
        print ("You drew the " + number(randomnumber)+ " of " + suit(randomsuit) +"!")

CardDeck.DrawCard()


Each time I try this, the IDLE returns: "NameError: global name 'number' is not defined". Just for kicks, I tried putting the number and suit arrays in the DrawCard() function, and instead I get the error: "TypeError: 'list' object is not callable"

Any advice would be appreciated

Lygris
Newbie Poster
14 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Use "self"

import random
class CardDeck(self):
    self.suit = ["Spades","Clubs","Hearts","Diamonds"]
    self.number = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
 
    def DrawCard(self):
        randomsuit = random.randrange(0,3)
        randomnumber = random.randrange(0,12)
        print ("You drew the " + self.number[randomnumber]+ " of " + self.suit[randomsuit] +"!")
 
CardDeck.DrawCard()


It's not tested, but should be worked.

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

Here is a little upgrade to your code.

import random
class Deck():
    def __init__(self):
        self.suit = ["Spades","Clubs","Hearts","Diamonds"]
        self.number = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
    def DrawCard(self):
        randomsuit = random.randrange(0,3)
        randomnumber = random.randrange(0,12)
        print ("You drew the " + self.number[randomnumber]+ " of " +    self.suit[randomsuit] +"!") # Use "[]" in stead of "()" when using lists.  

x = Deck() # Create a new "Deck"
x.DrawCard() # Uses DrawCard


A few things to note.
~ Use brackets[] instead of parenthesis() when using lists.
~When defining functions in a class, you must put "self" 1st in the parenthesis.
There are a few other things, but I am not sure how to explain them.

redyugi
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 15
Solved Threads: 30
 

Yup, I forgot init in my code. Sorry :)

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

NameError: name 'self' is not defined

Do I have to declare a fresh object?

Lygris
Newbie Poster
14 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Beautiful, I got it. Thanks for your help, fellas!

Lygris
Newbie Poster
14 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

NameError: name 'self' is not defined

Do I have to declare a fresh object?

How do you get that error? Any one else ever have that error?

redyugi
Junior Poster in Training
80 posts since Jul 2009
Reputation Points: 15
Solved Threads: 30
 
How do you get that error? Any one else ever have that error?

My code is not correct, in a previous post saying that I forgot "init".
Your code is correct. :)

Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: