-1

This is my code for bot-card game.

card_5 = 3 #all variable names
card_6 = 4
card_7 = 5
card_8 = 6
player_card_list = [card_5,card_6,card_7,card_8]#list of variables
min_player_card = int(min(player_card_list))#i want this variable to print the variable name
max_player_card = int(max(player_card_list))
Jukezy
  • 1
  • 1
  • Mandatory link to [Ned Batchelder](https://nedbatchelder.com/text/names.html) – quamrana Aug 20 '23 at 15:29
  • 3
    `player_card_list` is exactly the same, in every conceivable respect, as `[3, 4, 5, 6]`. Nothing can tell what what the variable name was, because no variable names are being stored - lists contain only *values*. – jasonharper Aug 20 '23 at 15:29
  • You can't do it this way. Instead, you'll have to make a dictionary, which allows you to keep _names_ and _values_, as the link posted by @Brian61354270 says. – John Gordon Aug 20 '23 at 15:30
  • Is there some standard relationship between card name and value? For instance, why is "card_5" a 3? You may be able to use string formatting to convert value to name. Conversely, why are you naming each card? Why isn't it just a list in the first place and then using list indexing instead of "card_5" (which would be `card[0]` in this example)? – tdelaney Aug 20 '23 at 15:40

1 Answers1

1

If you want a name associated to a value, you're looking for a dict, not a list. Consider

player_card_dict = {
    "card_5": card_5,
    "card_6": card_6,
    "card_7": card_7,
    "card_8": card_8,
}

Then we can use the optional key argument on min or max to control how the comparison is done.

min_player_card = min(player_card_dict, key=lambda name: player_card_dict[name])
max_player_card = max(player_card_dict, key=lambda name: player_card_dict[name])
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • To avoid extra lookups, why not `min` or `max` of `player_card_dict.items()` by `itemgetter(1)` and drop the value? – STerliakov Aug 20 '23 at 17:41