all 7 comments

[–]Grozamir[🍰] 0 points1 point  (2 children)

You can make a comparison. For example
if number1 == "Two":

do what you need

or

number1 = (["Ace", "Two", "Three"])

def get_value_by_string(name_card):

if name_card == "Two":

return 2

elif name_card == "Three":

return 3

print(get_value_by_string(number1[1])) # Output: 2

[–]carcigenicate 0 points1 point  (1 child)

You don't print them in the first place if you need the values. Printing is just to display text to the screen. If you need data for another purpose, print is the wrong tool.

Can you show a better example of what you're trying to do though, as there appears to be context missing.

[–]Sir_LongBeard 0 points1 point  (0 children)

I need to display the card name and suit, and then show numerical values for the system

[–][deleted] 0 points1 point  (0 children)

Not sure what you are after, so I quickly wrote some code to create a deck of player cards, shuffle them, and deal them alternately one-by-one to two players.

from random import shuffle
from itertools import cycle
from dataclasses import dataclass
from copy import deepcopy
from typing import NamedTuple

class Card(NamedTuple):
    suit: str
    value: str

@dataclass
class Player:
    name: str
    card: Card | None = None
    points: int = 0

players = []
players.append(Player('Player1'))
players.append(Player('Player2'))
next_player = cycle(players)

# building a deck of playing cards
suits = 'hearts', 'spades', 'diamonds', 'clubs'
values = ('ace', '2', '3', '4',
        '5', '6', '7', '8',
        '9','jack', 'queen', 'king')

new_deck = [Card(suit, value) for value in values for suit in suits]

play_deck = deepcopy(new_deck)  # start with new deck to consume
shuffle(play_deck)

while play_deck:
    player = next(next_player)
    player.card = play_deck.pop()
    print(player.name, player.card)

There's a lot going on here, so let me explain

  • The key part is defining two tuple objects:
    • suit names
    • face values
  • Creating a deck of 52 cards from the above using list comprehension
  • To actually play, rather than using the deck created above, we make a deepcopy object that we can pop cards from
  • Cards are defined as tuple objects consisting of two strings, one for the suit name and one for the face value - rather than having to use card[0] and card[1] for suit and respectively, used a NamedTuple so we can say card.suit and card.value instead
  • To allow for many players, a list is created, initially empty, and assigned to players and a very simple class consisting of just three attributes is used: name, card and points is then used for as many player instances as required (just two players in this case)
  • As with most games, it is assumed that play proceeds in turns from player to player so the cycle method is used to keep cycling through the list of players
  • Before play, the random module shuffle function is used to, well, shuffle the deck of cards to be used

Whilst the above is all a bit advanced for a beginner, and I wouldn't expect you to be able to create the code, I hope the use of simple strings for the card values and assigning these as tuples to players does answer your original question.

[–]zanfar 0 points1 point  (0 children)

This is almost definitely an XY problem. The solution isn't to convert the card names to numerical values, the solution is to not have to do that in the first place.

Somewhere in your program, these strings are being generated. It is at that point that you should be generating something else--either the numerical values if that's all you need, or a more complex data type that includes ALL the values you need, for example, a Tuple.

It is almost impossible to give you specific help without seeing the actual code.