Python dictionary TypeError

When I access the dictionary in my Python blackjack game:

import random
deck= {'K ♥':'10' ,'Q ♥':'10','J ♥':'10','10 ♥':'10','9 ♥':'9','8 
♥':'8','7 ♥':'7','6 ♥':'6','5 ♥':'5','4 ♥':'4','3 ♥':'3','2 ♥':'2','A ♥':'11','K◆':'10','Q◆':'10','J◆':'10','10◆':'10','9◆':'9','8◆':'8','7◆':'7','6◆':'6','5◆':'5','4◆':'4','3◆':'3','2◆':'2','A◆':'11','K ♣':'10','Q ♣':'10','J ♣':'10','10 ♣':'10','9 ♣':'9','8 ♣':'8','7 ♣':'7','6 ♣':'6','5 ♣':'5','4 ♣':'4','3 ♣':'3','2 ♣':'2','A ♣':'11','K ♠':'10','Q ♠':'10','J ♠':'10','10 ♠':'10','9 ♠':'9','8 ♠':'8','7 ♠':'7','6 ♠':'6', '5 ♠':'5','4 ♠':'4','3 ♠':'3','2 ♠':'2','A ♠':'11'}
number= random.choice(list(deck.keys()))
del deck[number]
number2= random.choice(list(deck.keys()))
print(number)
print(number2)
value1 = int(deck.get(number))
value2 =int(deck.get(number2))
print(value1+value2)

It keeps saying there is a typeerror, I cannot add a none type or a string:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

How do I fix this?

Upvotes: -1

Views: 203

Answers (2)

Bala Sai Sigireddy
Bala Sai Sigireddy

Reputation: 11

The code provided is somewhat complex and it is rather simple to use random.sample function.

Instead of deleting as both do the same operation:

import random

deck = {
    'K ♥':10, 'Q ♥':10, 'J ♥':10, '10 ♥':10, '9 ♥':9, '8 ♥':8,
    '7 ♥':7, '6 ♥':6, '5 ♥':5, '4 ♥':4, '3 ♥':3, '2 ♥':2, 'A ♥':11,
    'K ◆':10, 'Q ◆':10, 'J ◆':10, '10 ◆':10, '9 ◆':9, '8 ◆':8,
    '7 ◆':7, '6 ◆':6, '5 ◆':5, '4 ◆':4, '3 ◆':3, '2 ◆':2, 'A ◆':11,
    'K ♣':10, 'Q ♣':10, 'J ♣':10, '10 ♣':10, '9 ♣':9, '8 ♣':8,
    '7 ♣':7, '6 ♣':6, '5 ♣':5, '4 ♣':4, '3 ♣':3, '2 ♣':2, 'A ♣':11,
    'K ♠':10, 'Q ♠':10, 'J ♠':10, '10 ♠':10, '9 ♠':9, '8 ♠':8,
    '7 ♠':7, '6 ♠':6, '5 ♠':5, '4 ♠':4, '3 ♠':3, '2 ♠':2, 'A ♠':11
}

card1, card2 = random.sample(list(deck.keys()), 2)
value1 = deck.get(card1, 0)
value2 = deck.get(card2, 0)

print(card1)
print(card2)
print(value1 + value2)

The sample output looks like this:

A ♥
9 ◆
2

Upvotes: 1

cdlane
cdlane

Reputation: 41925

What you're doing is basically correct but you have some of your operations in the wrong order. You need to get the first card's value before you delete it from the deck, not after. My rework of your code:

import random

deck = {
'K ♥':10, 'Q ♥':10, 'J ♥':10, '10 ♥':10, '9 ♥':9, '8 ♥':8,
'7 ♥':7, '6 ♥':6, '5 ♥':5, '4 ♥':4, '3 ♥':3, '2 ♥':2, 'A ♥':11,
'K ◆':10, 'Q ◆':10, 'J ◆':10, '10 ◆':10, '9 ◆':9, '8 ◆':8,
'7 ◆':7, '6 ◆':6, '5 ◆':5, '4 ◆':4, '3 ◆':3, '2 ◆':2, 'A ◆':11,
'K ♣':10, 'Q ♣':10, 'J ♣':10, '10 ♣':10, '9 ♣':9, '8 ♣':8,
'7 ♣':7, '6 ♣':6, '5 ♣':5, '4 ♣':4, '3 ♣':3, '2 ♣':2, 'A ♣':11,
'K ♠':10, 'Q ♠':10, 'J ♠':10, '10 ♠':10, '9 ♠':9, '8 ♠':8,
'7 ♠':7, '6 ♠':6, '5 ♠':5, '4 ♠':4, '3 ♠':3, '2 ♠':2, 'A ♠':11
}

card1 = random.choice(list(deck))
value1 = deck.get(card1)
del deck[card1]

card2 = random.choice(list(deck))
value2 = deck.get(card2)

print(card1)
print(card2)
print(value1 + value2)

OUTPUT

> python3 test.py
A ◆
9 ♥
20
>

Upvotes: 1

Related Questions