Reputation: 1
I'm trying to write a simple BlackJack program that works on random module, so it picks between the list of cards, and if the picked number belongs to the last 3 variables which are equal to 10, then it goes into an if statement where it decides if the card should king,queen or jack. At the end of if statements I have return statements which should make sure that the function's value when called upon should be the tuples, but when I try to call inside a while statement, I get " line 43, in card_name_x, card_name_y = card_conversion(x,y) ^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot unpack non-iterable NoneType object" because I think function returns None so it can't assign a value to the tuples.
import random
running = True
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
blackjack = ["jack", "king", "queen"]
x = cards[random.randint(0,12)]
y = cards[random.randint(0,12)]
player_deck = []
dealer_deck = []
def card_conversion(x,y):
if x == 10 and y == 10:
card_name_x = blackjack[random.randint(0, 2)]
print(card_name_x)
card_name_y = blackjack[random.randint(0, 2)]
print(card_name_y)
return card_name_x, card_name_y
elif x != 10 and y == 10:
card_name_y = blackjack[random.randint(0, 2)]
card_name_x = x
print(card_name_y)
return card_name_x, card_name_y
elif x == 10 and y != 10:
card_name_x = blackjack[random.randint(0, 2)]
card_name_y = y
print(card_name_x)
return card_name_x, card_name_y
def card_point_total():
sum = x + y
return sum
player_turn = True
dealer_turn = True
while running:
if dealer_turn == True:
card_conversion(x,y)
card_name_x, card_name_y = card_conversion(x,y)
card_point_total()
dealer_deck.append(card_name_x)
dealer_deck.append(card_name_y)
print(card_point_total())
print(dealer_deck)
dealer_turn = False
#player_turn = True
if player_turn == True:
card_conversion(x,y)
card_name_x, card_name_y = card_conversion(x,y)
player_deck.append(card_name_x)
player_deck.append(card_name_y)
print(card_point_total())
print(player_deck)
player_turn = False
#dealer_turn= True
Upvotes: -4
Views: 53
Reputation: 18866
You function doesn't define an else:
clause for the if statements, so if none of the conditions are True
(namely, when neither x
nor y
equals 10), your function card_conversion
will end and return None
as a default
Either expressly return
something valid at the end or raise
to trap this exceptional case!
>>> def test(a):
... if a == 1:
... return 1
... elif a == 2:
... return 2
... raise RuntimeError(f"bug: unexpected value for a: {a}")
...
>>> test(1)
1
>>> test(2)
2
>>> test(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in test
RuntimeError: bug: unexpected value for a: 3
Upvotes: 3