Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python blackjack program code issue. Why is it returning key "1" instead of "10"?

I used python tutor to step through the code and found that there is a bug in user_current_score(). for cards.get(card), it is pulling the "10" dictionary key but seeing it as a 1 instead, then returning None since there is no 1 card. Why would it be seeing a 1 card in the dictionary when there isn’t one there? I know I could do this differently, but I still want to know why it’s pulling the "10" key as a "1" instead and returning "None". If I comment the "10" dictionary key value pair out, there’s no problem with the code so far. If it pulls a 10 though, I get this error:

10
 
None
Traceback (most recent call last):
  File "main.py", line 146, in <module>
    user_current_score()
  File "main.py", line 133, in user_current_score
    user_score += cards.get(card)
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

or

K
10
 
10
None
Traceback (most recent call last):
  File "main.py", line 146, in <module>
    user_current_score()
  File "main.py", line 133, in user_current_score
    user_score += cards.get(card)
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
îş§ 

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import random
cards = {
  "A":11,
  "2":2,
  "3":3,
  "4":4,
  "5":5,
  "6":6,
  "7":7,
  "8":8,
  "9":9,
  "10":10,
  "J":10,
  "Q":10,
  "K":10
}
user_hand = []

comp_hand = []
comp_score = 0
temp_hand = []
translation = {39: None}

def deal_card():
  temp_hand = []
  temp_hand = random.choice(list(cards))
  print(temp_hand) #for testing, needs removed
  return temp_hand

def user_current_score():
  user_score = 0
  for card in user_hand:
    print(cards.get(card))  #for testing, needs removed
    user_score += cards.get(card)
  print("Your cards:", str(user_hand).translate(translation) + f", current score: {user_score}")
  return user_score

def comp_current_score():
  comp_score = 0
  for card in comp_hand:
    comp_score += cards.get(card)
  return comp_score  

while len(user_hand) < 5:
  user_hand += deal_card() 
print(" ")
user_current_score()

>Solution :

You should use append method to add values to your lists.

When you try to += list with str (with more than 1 character) you are actually splitting this str to characters and append them sequentally.

try this one:

while len(user_hand) < 5:
  user_hand.append(deal_card()) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading