我试着制作一个不和谐的机器人,为了好玩而播放美国大选,我已经得到了所有的东西,除了它打印的分数
@client.command()
async def ping(ctx):
global Biden
global Trump
while Biden > Trump:
await ctx.send('Biden"s winning')
await ctx.send('Trump, '.Trump)
await ctx.send('Biden, '.Biden)
time.sleep(5)
if (Biden == 270):
for i in (0, 5):
await ctx.send('Biden WON')
break
else:
await ctx.send('Trump"s winning')
await ctx.send('Trump, '.Trump)
await ctx.send('Biden, '.Biden)
time.sleep(5)
if (Trump == 270):
for i in (0, 5):
await ctx.send('Trump won')
break我只是想不出怎么让机器人发送这两个变量
发布于 2020-11-05 15:51:33
好吧,我修复了它,包括它没有更新变量的事实!
@client.command()
async def ping(ctx):
while 1 :
Trumplists = soup.find('div', class_='ge-bar__count ge-bar__count--p color--R')
Bidenlists = soup.find('div', class_='ge-bar__count ge-bar__count--p color--D')
trumpstr = Trumplists.text.strip()
bidenstr = Bidenlists.text.strip()
Trump = int(trumpstr)
Biden = int(bidenstr)
if Biden > Trump:
await ctx.send('')
await ctx.send('Biden, ' + str(Biden))
await ctx.send('Trump, '+ str(Trump))
time.sleep(600)
if (Biden == 270):
for i in (0, 5):
await ctx.send('')
break
else:
await ctx.send('')
await ctx.send('Trump, '+ str(Trump))
await ctx.send('Biden, '+ str(Biden))
time.sleep(600)
if (Trump == 270):
for i in (0, 5):
await ctx.send('')
break这应该就行了
发布于 2020-11-04 20:04:17
根据ctx.send()文档,此方法只接受一个位置参数、一个字符串和要发送的消息。
替换
await ctx.send('Trump, '.Trump)
await ctx.send('Biden, '.Biden)使用
await ctx.send('Trump, ' + str(Trump))
await ctx.send('Biden, ' + str(Biden))来制作你想要的字符串。构造字符串的替代方法如下:
# f-strings
f"Trump, {Trump}"
# string formatting
"Trump, {}".format(Trump)
# string formatting with %
"Trump, %d" % Trump
# string concatenation
"Trump, " + str(Trump)发布于 2020-11-04 20:11:06
您的代码有几个问题。首先,你不是在1-5之间循环,而是在一个元组中循环。您必须使用range。
对于格式化字符串,您可以使用
await ctx.send(f'Trump, {Trump}')或
await ctx.send('Trump, {0}'.format(Trump))。
@client.command()
async def ping(ctx):
global Biden
global Trump
while Biden > Trump:
await ctx.send('Biden"s winning')
await ctx.send(f'Trump, {Trump}')
await ctx.send(f'Biden, {Biden}')
time.sleep(5)
if Biden == 270:
for i in range(0, 5):
await ctx.send('Biden WON')
break
else:
await ctx.send('Trump"s winning')
await ctx.send(f'Trump, {Trump}')
await ctx.send(f'Biden, {Biden}')
time.sleep(5)
if Trump == 270:
for i in range(0, 5):
await ctx.send('Trump won')
breakhttps://stackoverflow.com/questions/64679373
复制相似问题