0

Needless to say the following code does not work and the fault or problem seems to be that the function does not access or recognise the list [cards]. On the other hand if I place the list [cards] within the function, the code works perfectly. I had assumed that variables placed in the main code were global, and variables declared in a function were just local.

#!/usr/bin/python

import random

cards = ['A︎♣︎︎', '2︎♣︎︎', '3︎♣︎︎', '4︎♣︎︎', '5︎♣︎︎', '6︎♣︎︎', '7︎♣︎︎', '8︎♣︎︎', '9︎♣︎︎', '10︎♣︎︎', 'J︎♣︎︎', 'Q︎♣︎︎',
         'K︎♣︎︎', 'A♠︎', '2♠︎', '3♠︎', '4♠︎', '5♠︎', '6♠︎', '7♠︎', '8♠︎', '9♠︎', '10♠︎', 'J♠︎',
         'Q♠︎', 'K♠︎', 'A︎♥︎', '2︎♥︎', '3︎♥︎', '4︎♥︎', '5︎♥︎', '6︎♥︎', '7︎♥︎', '8︎♥︎', '9︎♥︎', '10︎♥︎',
         'J︎♥︎', 'Q︎♥︎', 'K︎♥︎', 'A︎♦︎︎', '2︎♦︎︎', '3︎♦︎︎', '4︎♦︎︎', '5︎♦︎︎', '6︎♦︎︎', '7︎♦︎︎', '8︎♦︎︎', '9︎♦︎︎',
         '10︎♦︎︎', 'J︎♦︎︎', 'Q︎♦︎︎', 'K︎♦︎︎']

# define function


def sort_cards(hand):

    temp = []
    for n in range(0, 7):
        temp.append(cards.index(str(hand[n])))

    # sort cards
    temp.sort()
    hand = []

    # fetch card according to index and assign to hand
    for c in temp:
        hand.append(cards[c])

    return hand


# copy cards list to working list

rem_cards = cards

# initiate players card list

player1 = []
player2 = []
player3 = []

# define variable
# 7 cards per player

deal = 7

while deal != 0:

    # get a card from rem_cards and assign to player1
    card = rem_cards[random.randint(0, len(rem_cards) - 1)]
    player1.append(card)
    # remove card from deck
    rem_cards.remove(card)

    card = rem_cards[random.randint(0, len(rem_cards) - 1)]
    player2.append(card)
    rem_cards.remove(card)

    card = rem_cards[random.randint(0, len(rem_cards) - 1)]
    player3.append(card)
    rem_cards.remove(card)

    deal -= 1


print(sort_cards(player1))
print(sort_cards(player2))
print(sort_cards(player3))
print("No of cards left in the deck is ", len(rem_cards))

Any suggestions or is my concept just wrong?

Pugwash
  • 48
  • 5
  • 4
    what does 'not work' mean? do you get an error or unexpected output? – matias elgart Nov 13 '16 at 10:30
  • If you wish to use global variable inside function you should declare that variable in function body with `global` keyword – Tomasz Plaskota Nov 13 '16 at 10:31
  • Just a tip: It's probably cleaner to use `random.choice` rather than indexing with `random.randint(0, len(rem_cards) - 1)` – juanpa.arrivillaga Nov 13 '16 at 10:43
  • Or `random.sample`, since you want several random items at once. – Blckknght Nov 13 '16 at 10:51
  • One suggestion, unrelated to the issue you're having (which the answers below seem to have addressed): Your `sort_cards` function could be dramatically simplified to `hand.sort(key=cards.index)` (or maybe `return sorted(hand, key=cards.index)` if you need to copy it rather than sorting in place). – Blckknght Nov 13 '16 at 10:54
  • @Pugwash I like your cards, nice try – Serjik Nov 13 '16 at 10:58

3 Answers3

1

Take a look at your comments:

# copy cards list to working list

rem_cards = cards

This code does not make a copy of the list, it creates another name, under which the original list could be accessed. Whenever you modify rem_cards, cards is modified. Thus, rem_cards.remove(card) actually removes the card from cards!

If you want to copy the list, use copy.[deep]copy:

import copy

# copy cards list to working list

rem_cards = copy.deepcopy(cards) # if cards could contain other lists
rem_cards = copy.copy(cards) # create a simple shallow copy
rem_cards = cards[:] # you may create shallow copies without the copy module
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

This also will solve your problem without using copy.deepcopy

rem_cards = cards[:]
Serjik
  • 10,543
  • 8
  • 61
  • 70
0
rem_cards = cards

does not copy the list, but just creates an alias. It should be

rem_cards = list(cards)
SergiyKolesnikov
  • 7,369
  • 2
  • 26
  • 47