PYTHON: AttributeError: 'int' object has no attribute 'hand'
<p>So I have an exercise on python - building a BlackJack game.
I have started with defining how every phrase of the game would go.
Now when I run this code below, In case the input is '0' - which means you don't want cards anymore, it runs perfectly. But when the input is '1' - which means you want to pick card, I get an error:</p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 1, in &lt;module&gt;
    class blackjack(object):
  File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 34, in blackjack
    player(1)
  File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 25, in player
    PickCard(1)
  File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 18, in PickCard
    self.hand+= random.randrange(1, 13)
AttributeError: 'int' object has no attribute 'hand'
</code></pre>

<p>The code:</p>

<pre><code>class blackjack(object):

    #this func defines how player should react
    def player(self):

        #this func defines what case of technical loosing
        def loser():
            print("You have reached", hand , ". Which is beyond 21. Therefor, you have lost the game. Better luck next time!")


        #this func is responsible for picking card issue.
        def PickCard(self):
            import random
            x=1
            while x == 1:
                pick = int(raw_input("Enter 1 if you want another card, else Enter 0"))
                if pick == 1:
                    self.hand = self.hand + random.randrange(1, 13)
                else:
                    x=0
        import random
        print "Now your first card will automatically be given to you:"
        hand=random.randrange(1,13)
        print "hand: ", hand
        PickCard(1)
        print hand
        if hand&gt;21:
            loser()
        elif hand==21:
            pass
        else:
            pass

    player(1)
</code></pre>

<p>Thanks in advance.</p>

<p>You are making the function call as <code>player(1)</code> where the <code>player</code> function expects the argument as of <code>self</code> type .i.e. instance of the class <code>blackjack</code>. Hence, while doing <code>self.hand = self.hand + random.randrange(1, 13)</code> it is throwing the above mentioned error.</p>

<hr>

<p><em>I think</em> you do not want to make a call to <code>player()</code> function from within the class, Is it? Move that part to outside the class. Firstly create the object of class <code>blackjack</code> (Note: In Python, class names should be defined as CamelCase variables like: BlackJack). For example:</p>

<pre><code>blackjack_obj = blackjack()
</code></pre>

<p>Then call the <code>player()</code> function as:</p>

<pre><code>blackjack_obj.player()
</code></pre>

python python-2.7 int attributeerror