首页 > 解决方案 > 如何让我的机器人判断给出的数字是高于还是低于答案?[不和谐.py]

问题描述

我正在做一个数字猜谜游戏,我想知道如何让它告诉你你的答案是高于还是低于答案?到目前为止,这是我的代码:

async def number(ctx):
    await ctx.send('I\'m thinking of a number from one to ten. Try to guess which number!')
    number = random.randint(1, 10)
    answer = await client.wait_for('message', check=lambda message: message.author == ctx.author and message != "")
    answer = answer.content

    loop_counter = 0
    while answer.lower() != number:
        loop_counter += 1
        if loop_counter >= 2:
            await ctx.send(f'sorry dude, the correct number was {number}')
            break
        await ctx.send('Incorrect, try again')
        answer = await client.wait_for('message', check=lambda message: message.author == ctx.author and message != "")
        answer = answer.content

        if answer.lower() == number:
            "[testing]" ```
Thanks!

标签: pythondiscordbotsdiscord.py

解决方案


在您的while循环中,您也可以添加一个if-else循环。

while answer.lower() != number:
    if answer.lower()<number:
        #Do things if the number is lower
    else: 
        #Do things if the number is higher
    loop_counter += 1
    if loop_counter >= 2:
        await ctx.send(f'sorry dude, the correct number was {number}')
        break

注意:虽然通常当您检查数字是否更高/更低时,您无法使用else,因为数字可能相等,但如果它们相等,则循环不会开始,您可以else在此处使用


推荐阅读