#πŸ”’ Is there a way to print just the name of a variable?

50 messages Β· Page 1 of 1 (latest)

elder sentinel
#

I want to make it so I can print something along the lines of

" 'variable name' bla bla bla 'data' "
i only have 2 sets of variables so i could just use if this print this = this elif that = that but i was wondering if i could just simplify it into one line or so

abstract lotusBOT
#

@elder sentinel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

elder sentinel
#

specifically im trying to show who draws what card and i have this print(f"Player draws {self.hand.cards[-1]}\n") lined out but i wanna have it say player or dealer depending

cold burrow
#

don't you have another variable that is used to specify whether it's the player or the dealer that have drawn?

elder sentinel
#

how do you mean

cold burrow
#

I mean, I assume you have something like


to_play = 'player'

repeat until end of game:
  if to_play == "player": player draws
  else: dealer draws
  switch to_play
check success
#

so you can use the to_play variable to know which one is drawing

#

like, this is something you are already aware of,, bc both player and dealers don't play together

elder sentinel
#

i use the same class method to handle taking cards from the deck tho

#

whats the paste bin thing again

cold burrow
#

can you show me some code ?

#

!code

abstract lotusBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

elder sentinel
#

from deck class

    def draw(self):
        return self.cards.pop()

from pile class

    def add(self, card):
        self.cards.append(card)
cold burrow
#

what is self here ?

#

ok

#

wait why do you add cards back to the deck ?

#

ok

#

nvm

elder sentinel
#

i keep hitting enter lol sorry

cold burrow
#

right so that's not in this that you'll gonna print antyhing

elder sentinel
#

im trying to take bits in context cuz theres alot atp

cold burrow
#

can you just paste the whole code in a pastebin

#

and point to where you would like to print the variable name

elder sentinel
cold burrow
elder sentinel
#

oh pfft i saw the discord ``` formatting and my eyes glossed over

cold burrow
#

no prob

elder sentinel
#

first one is card handling and second is the actual game you can ignore the rough method its my rough draft im cutting up to try to fit into a class better at least i think im doing it better lol

cold burrow
#

alright, so where do you want to print the variable name ?

elder sentinel
#

the dealt method

#

im wanting to be able to call it similar to the score method where it can be used for anyone and work w that specifically

#

tho score just returns a number and i know who im calling it for so a little different

elder sentinel
# elder sentinel the dealt method

where it says rn on line 107 "player draws..." i wanna be able to put either dealer or player without copying the line twice with if statements

cold burrow
#

you can do something like



    def dealt(self, who: str):
        hand = self.player if who =="player" else self.dealer
        print(f"{who.capitalize()} draws {hand.cards[-1]}\n")

#

and call the function with the appropriate argument

#

but like, you can't from a variable get its name

elder sentinel
#

damn thats alot cleaner than what i was gonna go for but kinda what i was trying to avoid aswell lol

cold burrow
#

names are only identifiers and they can even change, for example, assuming such function nameof exists:


def print_name(x):
    print(nameof(x))

y = 5
print_name(y) # do you expect it to be "y" or "x" ?
#

when you code, you are already aware of the name of the variable you use

elder sentinel
#

fair also .capitalize will only caps the first letter right?

cold burrow
#

IIRC yes

elder sentinel
#

baller thats a new one for me lol thank you

cold burrow
#

np

elder sentinel
#

add that to my mental repository lol

cold burrow
#

at the end it's a detail

elder sentinel
#

always nice to have outputs that look nice lol

abstract lotusBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.