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
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