I'm relatively new to python but think I have a decent enough understanding, except for (apparently) the correct way to use the "import" statement. I assume that's the problem, but I don't know.
I have
from player import player
def initializeGame():
player1 = player()
player1.shuffleDeck()
player2 = player()
player2.shuffleDeck()
and
from deck import deck
class player(object):
def __init__(self):
self.hand = []
self.deck = deck()
def drawCard(self):
c = self.deck.cards
cardDrawn = c.pop(0)
self.hand.append(cardDrawn)
def shuffleDeck(self):
from random import shuffle
shuffle(self.deck.cards)
But when i try to initializeGame() it says "player1 has not been defined" and I'm not really sure why. In that same file if I just use "player1 = player()" then it woks perfectly fine but it refuses to work inside of a function. Any help?
EDIT: ADDING THINGS THAT WEREN'T INCLUDED BEFORE
class deck(object):
def __init__(self):
self.cards = []
def viewLibrary(self):
for x in self.cards:
print(x.name)
def viewNumberOfCards(self, cardsToView):
for x in self.cards[:cardsToView]:
print(x.name)
from deck import deck
class player(object):
def __init__(self):
self.hand = []
self.deck = deck()
def drawCard(self):
c = self.deck.cards
cardDrawn = c.pop(0)
self.hand.append(cardDrawn)
def shuffleDeck(self):
from random import shuffle
shuffle(self.deck.cards)
and the traceback error is
player1.deck.cards
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
player1.deck.cards
NameError: name 'player1' is not defined