Black Jack Game: Part 2 | Python
Would you believe me if I told you it was as simple as missing parentheses? It turns out that just a single set of parentheses is what caused all my troubles yesterday. A lesson earned is a lesson learned and I definitely earned this lesson by not being careful with my function calls versus function pointers.
By coming back to the problem today with a fresh mind and pair of eyes, it was pretty clear where the issue was though! Let’s take a look at the bug fix as well as the additional code to complete this Black Jack game.
First, I want to discuss my mistake and what I could have done to resolve this sooner. As you can see from the comparison above, the deal_card() function is missing its parentheses in user_cards.append(). By omitting the parentheses, I was pointing to the function deal_card() instead of calling it.
Effectively, instead of telling ‘user_cards’ to append the result returned from deal_cards() being run; I was telling ‘user_cards’ to append the code itself held within deal_cards() which was invalid the goal was to check that the result equaled 2.
Outside of this obvious typo, a lesson learned is to check the code outside of the function providing the error. I was previously convinced that the error had something to do with the code held within the deal_cards() function itself. However, I now know this function is where the problem failed, but because of faulty data being passed from elsewhere.
The moral of the story is to strategically check your entire project.
Next, I defined a new function named compare(), which takes two arguments of ‘user_score’ and ‘computer_score’. If both ‘user_score’ and ‘computer_score’ are greater than 21, a loss message is returned. Otherwise, the user’s score and computer’s scores are compared against the remaining possibilities and their corresponding win/loss feedback is returned.
Next, I defined a function named play_game() which holds all of the logic from yesterday’s post plus some. In addition to moving the previously defined lists within this function, I also added a new ‘while’ loop. This loop checks whether the computer’s score is both less than 17 and not 0 (indicating a Black Jack). So long as this condition is true, it forces the computer to take another card in its hand.
After the two previously discussed print statements revealing both the user and computer hands and final scores, the compare() function is called to print the final verdict on who won.
Finally, while the user’s input to the above prompt is equal to “y”, the terminal is cleared via the clear() function and play_game() is called again restarting the game.
I certainly did not anticipate that this project would take two days to complete initially. However, as with most things in life, it’s beneficial to be a little flexible with your expectations. It’s also worth not beating yourself up over things so hard. This was such a small mistake that caused me a good bit of frustration yesterday, that in the end, I did feel a little inadequate. But, I have to remember that I am still a newbie in Python. This project has significantly more logic than the previous 8, so it’s a growing pain and not an indication of my capabilities.
You can view and download the final source code for this project on my GitHub by clicking here. Thank you for your time!