Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - How do I convert a mention variable, to a normal variable and print it to my terminal

I have a variable in my code that looks like this, only problem is it looks very ugly in my terminal. Here's a picture.

My temrminal

I would like it to display: {Name of the winner} has won: {prize}, instead of showing their Discord ID and making my terminal look horrible to look at.

Here is my code, which makes this monstrosity of a terminal.

winners = random.sample([user for user in users if not user.bot], k=winerscount)

winnerstosend = "
".join([winner.mention for winner in winners])




win = await msg.edit(embed = discord.Embed(title = "WINNER" , description = f"Congratulations {winnerstosend}, you have won **{msg4.content}**!" , color = discord.Color.blue()))

print(Fore.GREEN + winnerstosend , Fore.WHITE + "has won: " , Fore.GREEN + msg4.content)
print(Style.RESET_ALL)

Now I would love it if you could help me out, and convert my mistake into a beautiful confirmation message.

question from:https://stackoverflow.com/questions/65903980/how-do-i-convert-a-mention-variable-to-a-normal-variable-and-print-it-to-my-ter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is user your object? If so, you can define a str dunder-method and print the string in the way you want.

Please see example below.

user@Inspiron:~/code/advanced_python$ cat str_dunder_example.py 
class A:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f'User {self.name} aged {self.age} has won'

if __name__ == '__main__':
    a = A('FakeBlob', 20)
    print(a)
user@Inspiron:~/code/advanced_python$ python str_dunder_example.py 
User FakeBlob aged 20 has won
user@Inspiron:~/code/advanced_python$ 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...