0
deck = ['1c', '4s', '8s', '8h', '1h', '2s', '2c', '8h', 'ks', 'qd', '4d', 'jd', '7c', '10h', '5c', '10d', '3d', '9c', '7d', '4h', '2s']
powerCard = "1c"
def deckStrength(powerCard, deck):
#     global deck
    powerCardExists = False
    for card in deck:
        if card == powerCard:
            powerCardExists = True
    if(powerCardExists):
        deck.remove(powerCard)
    for card in deck:
        card = card[:-1]
    print(deck)

deckStrength(powerCard, deck)

If you run this, the output will be:

['4s', '8s', '8h', '1h', '2s', '2c', '8h', 'ks', 'qd', '4d', 'jd', '7c', '10h', '5c', '10d', '3d', '9c', '7d', '4h', '2s']

As you can see, with the last for loop in my deckStrength function, I was trying to get rid of the last character in each string within my deck list. This didn't transpire, any clue as to why?

I also want to add that I tried doing this without deck as a parameter for the function and calling "global deck" but that didn't work so I tried this.

notacorn
  • 3,526
  • 4
  • 30
  • 60

1 Answers1

1

You need to create a new list and assign that list to your changes

For example:

deck = ['1c', '4s', '8s', '8h', '1h', '2s', '2c', '8h', 'ks', 'qd', '4d', 'jd', '7c', '10h', '5c', '10d', '3d', '9c', '7d', '4h', '2s']
powerCard = "1c"
def deckStrength(powerCard, deck):
#     global deck
    powerCardExists = False
    for card in deck:
        if card == powerCard:
            powerCardExists = True
    if(powerCardExists):
        deck.remove(powerCard)
    new_deck = [card[:-1] for card in deck]
    print(new_deck)

deckStrength(powerCard, deck)
chevybow
  • 9,959
  • 6
  • 24
  • 39