3

I am writing python code to interpret the data I collect while playing a game of clue. In a particular section of the code I am adding a list of cards to one of three different sets depending on which set corresponds to the cards in my hand. At this point I already have a list filled with 6 cards, like so...

standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']

These are the six cards I am attempting to add to my hand. In addition, I already have a dictionary of the three players involved in the game paired with their "player number," such as...

players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}

And finally, there are three empty sets, one set for the cards of each player...

player0_cards = set()
player1_cards = set()
player2_cards = set()

My objective is to (1) determine which of these three empty sets corresponds to the cards that are in my (Bob's) hand, and (2) add my list of six cards into that set.

The code that I have now, which to seems bad and "non-Pythonic" is as follows.

standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
player0_cards = set()
player1_cards = set()
player2_cards = set()
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
if players['Bob'] == 'Player 0':
 for card in standby_list:
 player0_cards.add(card)
elif players['Bob'] == 'Player 1':
 for card in standby_list:
 player1_cards.add(card)
elif players['Bob'] == 'Player 2':
 for card in standby_list:
 player2_cards.add(card)

Who knows maybe this is the best way to code for what I want the program to do, If not please let me know.

Thanks...

jpp
166k37 gold badges301 silver badges363 bronze badges
asked Jul 2, 2018 at 21:08
1
  • 2
    You've not done terribly by the way! Commented Jul 2, 2018 at 21:18

3 Answers 3

2

One Pythonic solution is to use a dictionary to store a variable number of variables. Resist the temptation to name each and every variable. Instead, group similar variables as key-value dictionary mappings.

Note also you can use set.update instead of adding each item of a list to a set sequentially.

Here's an example. Notice that we can feed the value returned by players['Bob'] directly to the cards dictionary. Since this returns a set, we can use set.update on the result.

standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
cards = {'Player 0': set(), 'Player 1': set(), 'Player 2': set()}
cards[players['Bob']].update(standby_list)
print(cards)
{'Player 0': set(),
 'Player 1': {'Candlestick', 'Hall', 'Mustard', 'Plum', 'Revolver', 'Study'},
 'Player 2': set()}
answered Jul 2, 2018 at 21:17
Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this

standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
cards = {'Player 0': set(), 'Player 1': set(), 'Player 2': set()}
cards[players['Bob']].add(tuple(standby_list))
print (cards[players['Bob']])
# {('Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall')}
answered Jul 2, 2018 at 21:20

1 Comment

The problem with this is that I would be adding a tuple to the set that corresponds to Bob's cards. Therefore, later on in the program/game when I am evaluating the "accusations" other players make, if I am trying to determine if a single card is in my hand, it will return a false value because it turns out the entire tuple of six cards is in my hand. I think @jpp is right, but thank you for your input! :)
1

You can initialize standby_list as a set (because cards are unique), players as a list, and use list comprehension to generate player_cards to make it Pythonic. Specifying "Player 1", "Player 2" and "Player 3" is redundant because the list index itself can represent the player number.

standby_list = {'Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall'}
players = ['Billy', 'Bob', 'Joe']
player_cards = [standby_list if player == 'Bob' else set() for player in players]

And player_cards will become:

[set(), {'Study', 'Revolver', 'Mustard', 'Plum', 'Hall', 'Candlestick'}, set()]

Your player number can then be an integer used to access the player_cards list of sets.

answered Jul 2, 2018 at 21:28

2 Comments

The issue here is that now all three players seem to have Mustard, Plum, Revolver, Candlestick, Study, and Hall in their hands and that is not possible.
@ZackBoi Oops I did not notice that you were looking to match "Bob" as a player to give him the cards. I just edited my answer accordingly. Please try it out.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.