Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the first part of my code for a blackjack simulator I'm making. The program is supposed to be one player playing with basic strategy against the dealer. Everything was working fine until I added the 'if' part of the deal_card(x) function.

So, if I run deal_card(phand), the set phand will have a card added to it. However, the if statement part won't affect phand at all. I'm trying to understand how the first part of the deal_card(x) function will affect whatever x is, but the latter (if) part doesn't change it.

number_of_decks = 6
standard_deck = sorted(4*[2,3,4,5,6,7,8,9,10,10,10,10,11])
shoe = sorted(number_of_decks*standard_deck)
removed=[]
results=[]
from random import randint
dhand=[]
phand=[]
def deal_card(x):
    card=shoe[randint(0,4*13*6-1-len(removed))]
    shoe.remove(card)
    removed.append(card)
    x.append(card)
    if sum(x)>21 and sorted(x)[len(x)-1]==11:
        x=sorted(x)
        x[len(x)-1]=1
        return x


This is so frustrating that it's making me lose my mind. So I'd be really grateful for anybody's help.

Thank you.

What I have tried:

I have tried Googling the answer but nobody really seems to be having the same issue I'm having.
Posted
Updated 12-Jun-18 19:55pm

1 solution

The actual reason why this happens is well explained in this blog[^], in the sections "Names and values" and "Assignment".

The solution for your immediate problem is to replace your call from e.g.
deal_card(mydeck)
to
mydeck = deal_card(mydeck)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900