Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:20:56+00:00 2026-06-15T09:20:56+00:00

I’m relatively new to python but think I have a decent enough understanding, except

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:20:57+00:00Added an answer on June 15, 2026 at 9:20 am
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        player1.deck.cards
    NameError: name 'player1' is not defined
    

    This shows the line where the error is thrown: player1.deck.cards. Said line is not in the code you gave us so we can only make assumptions on why you get the exception.

    However, it is very likely, that your script looks somehow like this:

    initializeGame()
    
    # and then do something with
    player1.deck.cards
    

    This however does not work, as player1 and player2 are only local variables inside the initializeGame function. As soon as the function returns no more references to them are left and they are most likely pending for garbage collection.

    So if you want to access those objects, you have to make sure that they stay around. You could do this by having the variables globally, or you could simply return them from your initializeGame function:

    def initializeGame():
        player1 = player()
        player1.shuffleDeck()
        player2 = player()
        player2.shuffleDeck()
        return player1, player2
    

    Then you can just call it like this:

    player1, player2 = initializeGame()
    

    And have local references to the created objects.

    Or even better, create an object that represents the whole game, where the players are instance variables:

    class Game:
        def __init__ (self):
            self.player1 = player()
            self.player1.shuffleDeck()
            self.player2 = player()
            self.player2.shuffleDeck()
    

    Then you can just create a Game instance and access the players using game.player1 or game.player2. And of course, having an object for the game itself allows you to encapsulate a lot game related functions into the object as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.