I am making a blackjack simulator with python and are having problems with when the player want another card. To begin with the player gets a random sample of two numbers from a list and then get the option to take another card or not to. When the answer is yes another card is added to the random sample but it gets added as a list inside of the list.
This is is the line when the answer is yes to another card.
if svar == "JA":
handspelare.append(random.sample(kortlek,1))
print(handspelare)
This returns, [5, 10, [13]] and it is this list inside of the list i want to get rid of so i can sum the numbers, any suggestions on how i can get rid of this?
CodePudding user response:
random.sample(kortlek,1)
random.sample
returns a list, so you end up append
ing a list to handspelare
(which creates the sublists).
You could change append
to extend
, but random.sample(..., 1)
is just random.choice
, so it makes more sense to use handspelare.append(random.choice(kortlek))
.
CodePudding user response:
you should create a deck then shuffle it
deck = [52 cards]
deck.shuffle()
then just draw off it like a normal deck in the real world
hand.append(deck.pop())
if len(deck) < 15: # or something
deck = [52 cards]
deck.shuffle()
CodePudding user response:
Use list concatenation rather than append.
handspelare = random.sample(kortlek,1)
append
will not unbundle its argument
a = [1]
a.append([2]) # [1, [2]]
a = [1]
a = [2] # [1, 2]