Python 如何将提及变量转换为普通变量并将其打印到终端
我的代码中有一个变量看起来像这样,唯一的问题是它在我的终端中看起来非常丑陋。这是一张照片 我希望它显示:{Name of the winner}have:{prize},而不是显示他们不和谐的ID,让我的终端看起来很恐怖 这是我的代码,它构成了一个畸形的终端Python 如何将提及变量转换为普通变量并将其打印到终端,python,python-3.x,discord,bots,discord.py,Python,Python 3.x,Discord,Bots,Discord.py,我的代码中有一个变量看起来像这样,唯一的问题是它在我的终端中看起来非常丑陋。这是一张照片 我希望它显示:{Name of the winner}have:{prize},而不是显示他们不和谐的ID,让我的终端看起来很恐怖 这是我的代码,它构成了一个畸形的终端 winners = random.sample([user for user in users if not user.bot], k=winerscount) winnerstosend = "\n".join([
winners = random.sample([user for user in users if not user.bot], k=winerscount)
winnerstosend = "\n".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)
现在,如果您能帮助我,并将我的错误转化为一条漂亮的确认信息,我将不胜感激。用户是您的目标吗?
如果是这样,您可以定义一个字符串并以您想要的方式打印该字符串
请参见下面的示例
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$
用户是您的对象吗?
如果是这样,您可以定义一个字符串并以您想要的方式打印该字符串
请参见下面的示例
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$
嗯,试试
winner。显示你的名字,而不是winner。提到嗯,试试winner。显示你的名字,而不是winner。提到我不明白你想问什么@winnerstosend对象的llClass是由您定义的?它不是一个类…您可以通过如上所示定义str,将其转换为一个类并以自定义方式打印出来。我不明白您想问什么@WinnerToSend对象的llClass由您定义?它不是一个类…您可以转换为一个类,并通过如上所示定义str以自定义方式打印它。