When I run this code it works as intended about 90% of the time. Then out of the blue it will inject a [‘1’, ‘0’]. The ‘1’ is not coming from the value of the ‘A’ key. I tested this with setting the value to 100 and it still placed the ‘1’.
>Solution :
Your error stems from the fact that, to add to a list, you don’t use +=, but append. The correct code is:
def random_cards(n):
dealt_cards = []
for _ in range(n):
dealt_cards.append(random.choice(list(cards.keys())))
print(dealt_cards)
This is because addition with lists is defined only with iterables, which are converted to a list, then the two lists are concatenated. That is, ['c'] + 'ab' == ['c'] + list('ab') == ['c'] + ['a', 'b'] == ['c', 'a', 'b'].
