![](/img/trans.png)
[英]How do i make a leaderboard for my invite tracker discord bot? discord.py
[英]How do I make my bot tell if the number given was higher or lower then the answer? [discord.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!
在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
首先, if answer.lower() == number:
你不能这样做,因为answer
是一个str
object 但number
是一个integer
。 所以你不能比较它们。 而且你也不必做wait_for
2 次。 您也可以将它添加到 while 循环中。 你可以使用这个:
@client.command()
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)
loop_counter = 0
while loop_counter < 2:
anwser = client.wait_for('message', check=lambda message: message.author == ctx.author)
try:
answ = int(answer.content) # here it will try to return answer to a integer.
except ValueError:
await ctx.send('Please just type number.') # if it's not integer, then it will pass and send a warning.
pass
if answ == number:
await ctx.send('You found the number.') # if user founds the answer, it will send a message and ends the loop.
return
else:
await ctx.send('Sorry, wrong answer. Answer is higher.') if number-answ<0 else await ctx.send('Sorry, wrong answer. Answer is lower.')
loop_counter += 1
await ctx.send(f"Sorry, you couldn't find the answer. Answer was {number}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.