1

I'm trying to code a card trick using a combination of Python and Powershell (I'm well aware that it could be done with either Python or Powershell, but I want to practice with both anyway). When doing the Python part of the trick, I'm running into an error about NoneType not having an attribute __getitem__. I think I found an answer for the question at this question here TypeError: 'NoneType' object has no attribute '__getitem__' but I did not really understand what the answers meant (I'm still a beginner at this stuff). Also, I found another question where someone was attempting to do what I was doing (How to assign each element of a list to a separate variable?), but everyone at that post merely said that what he was doing was unnecessary instead of giving an answer. Essentially, I want to do what he was trying, except for only the first variable.

Here is my code:

from random import shuffle
TheList = ["Ace of Spades", "Three of Hearts", "Five of Diamonds", "Seven of Clubs", "Nine of Spades", "Jack of Hearts", "King of Diamonds", "Two of Clubs", \
"Four of Spades", "Six of Hearts", "Eight of Diamonds", "Ten of Clubs"]
TheShownCards = shuffle(TheList)
TheChosenCard = TheShownCards[0]
print TheShownCards
print TheChosenCard

Here is the error I'm getting:

TypeError: 'NoneType' object has no attribute '__getitem__'

The print commands at the end are just for debugging, and I will remove them once I finish the script. Essentially, I'm trying to get the first item of list TheShownCards to be saved as the variable TheChosenCard. I know that this might be pointless and unnecessary (I read that other post where people told him not to), but my idea here is not just to get the info to complete the program, but also to learn how to do it the way requested. If it does prove to be too much of a hassle, I'll probably scrap the variable, but any help on the matter would be appreciated.

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

2

shuffle changes lists in place i.e. it does not return anything, it just shuffles the existing list. To fix this, you can do:

from random import shuffle

TheShownCards = ["Ace of Spades", "Three of Hearts", "Five of Diamonds", "Seven of Clubs", "Nine of Spades", "Jack of Hearts", "King of Diamonds", "Two of Clubs", "Four of Spades", "Six of Hearts", "Eight of Diamonds", "Ten of Clubs"]
shuffle(TheShownCards)
TheChosenCard = TheShownCards[0]

print TheShownCards
print TheChosenCard

which returns:

['Jack of Hearts', 'Four of Spades', 'Seven of Clubs', 'Ace of Spades', 'Nine of Spades', 'Eight of Diamonds', 'Ten of Clubs', 'Five of Diamonds', 'King of Diamonds', 'Two of Clubs', 'Three of Hearts', 'Six of Hearts']
Jack of Hearts

This shows that the TheShownCards list has been shuffled, and the first element in the TheShownCards list has been saved to TheChosenCard

Robert Valencia
  • 1,752
  • 4
  • 20
  • 36
1

random.shuffle works in-place, meaning that TheList is now shuffled and that None is returned into TheShownCards. This means that when you try to get element 0, you make a call equivalent to None.__getitem__(0), which doesn't exist. Just make TheChosenCard the result of picking element 0 from TheList.

Gareth Pulham
  • 654
  • 4
  • 10