Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your <code>Deck</code> cards are all strings:</p> <pre><code>self.Deck = ['J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10'] </code></pre> <p>Note the <code>'2'</code> and <code>'3'</code>, etc. strings.</p> <p>Yet you try to treat them as integers:</p> <pre><code>for x in self.hand: if x != ',': if x not in range(1, 11): </code></pre> <p><code>x</code> will <strong>never</strong> be in <code>range(1, 11)</code>, because all your values are strings. So the above <code>if</code> statements are <strong>always</strong> <code>True</code>.</p> <p>The next part is:</p> <pre><code>if x != 'A': self.value += 10 else: if (self.value + 11) &gt; 17: self.value += 1 else: self.value += 11 </code></pre> <p>When 'number' cards are drawn, the <code>not in range(1, 11)</code> is still true, they are not equal to <code>'A'</code>, so you count all cards as value 10.</p> <p>You'll need to rethink how to handle the number cards here. You can test for alphanumeric ordering:</p> <pre><code>if x &gt;= 'A': </code></pre> <p>would work for your numeric cards too, as all numbers sort before capitals in the ASCII standard. Use that instead of the <code>not in range(1, 11)</code> test.</p> <p>Next problem is that you are then trying to add up the value using strings when the cards are numeric:</p> <pre><code>else: self.value += x </code></pre> <p>but that adds up strings; a string value is <strong>always</strong> greater than numbers (in Python 3, comparing strings and numbers raises an error).</p> <p>Use <code>int()</code> to sum actual numbers:</p> <pre><code>else: self.value += int(x) </code></pre> <p>Now at least you have something working:</p> <pre><code>&gt;&gt;&gt; deck = Deck() &gt;&gt;&gt; jack = BlackJackPlayer(deck) &gt;&gt;&gt; jack.play_dealer() A A,A A,A,9 'BUST' &gt;&gt;&gt; jack.get_value() 60 </code></pre> <p>But this is still too high. That's because you use <code>self.value</code> to store the calculation but <em>never reset it</em>. You keep on adding for each new calculation.</p> <p>Use a local variable instead of <code>self.value</code>:</p> <pre><code>def get_value(self): value = 0 for x in self.hand: if x &gt;= 'A': if x != 'A': value += 10 else: if value &gt; 6: value += 1 else: value += 11 else: value += int(x) return value </code></pre> <p>Now the game works as expected:</p> <pre><code>&gt;&gt;&gt; jack = BlackJackPlayer(Deck()) &gt;&gt;&gt; jack.play_dealer() J JA JAA JAA6 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload