#π Is there a way to print just the name of a variable?
50 messages Β· Page 1 of 1 (latest)
@elder sentinel
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.
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
don't you have another variable that is used to specify whether it's the player or the dealer that have drawn?
how do you mean
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
i use the same class method to handle taking cards from the deck tho
whats the paste bin thing again
from deck class
def draw(self):
return self.cards.pop()
from pile class
def add(self, card):
self.cards.append(card)
i keep hitting enter lol sorry
right so that's not in this that you'll gonna print antyhing
im trying to take bits in context cuz theres alot atp
can you just paste the whole code in a pastebin
and point to where you would like to print the variable name
what was that link
here
oh pfft i saw the discord ``` formatting and my eyes glossed over
no prob
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
alright, so where do you want to print the variable name ?
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
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
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
damn thats alot cleaner than what i was gonna go for but kinda what i was trying to avoid aswell lol
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
fair also .capitalize will only caps the first letter right?
IIRC yes
baller thats a new one for me lol thank you
np
add that to my mental repository lol
at the end it's a detail
always nice to have outputs that look nice lol
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.