首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不一致机器人不从外部发送变量

不一致机器人不从外部发送变量
EN

Stack Overflow用户
提问于 2020-11-04 19:41:26
回答 3查看 42关注 0票数 0

我试着制作一个不和谐的机器人,为了好玩而播放美国大选,我已经得到了所有的东西,除了它打印的分数

代码语言:javascript
复制
@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

我只是想不出怎么让机器人发送这两个变量

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-11-05 15:51:33

好吧,我修复了它,包括它没有更新变量的事实!

代码语言:javascript
复制
@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

这应该就行了

票数 0
EN

Stack Overflow用户

发布于 2020-11-04 20:04:17

根据ctx.send()文档,此方法只接受一个位置参数、一个字符串和要发送的消息。

替换

代码语言:javascript
复制
await ctx.send('Trump, '.Trump)
await ctx.send('Biden, '.Biden)

使用

代码语言:javascript
复制
await ctx.send('Trump, ' + str(Trump))
await ctx.send('Biden, ' + str(Biden))

来制作你想要的字符串。构造字符串的替代方法如下:

代码语言:javascript
复制
# f-strings
f"Trump, {Trump}"

# string formatting
"Trump, {}".format(Trump)

# string formatting with %
"Trump, %d" % Trump

# string concatenation
"Trump, " + str(Trump)
票数 0
EN

Stack Overflow用户

发布于 2020-11-04 20:11:06

您的代码有几个问题。首先,你不是在1-5之间循环,而是在一个元组中循环。您必须使用range

对于格式化字符串,您可以使用

await ctx.send(f'Trump, {Trump}')

await ctx.send('Trump, {0}'.format(Trump))

代码语言:javascript
复制
@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')
                break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64679373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档