python - 如何让我的机器人判断给出的数字是高于还是低于答案?[不和谐.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
在此处使用
推荐阅读
- android - 谷歌地图视图因扩展片段类而损坏
- c++ - 在 C++ 上的 struct 内搜索对象
- html - li > 填充没有填充父 li div
- sql - Ecto / Elixir:返回嵌套关联的第一个孩子
- influxdb - 对 influxDB 进行 15m 和 30m 的平均统计
- python - 如何将列表应用于熊猫组
- angular - InvalidPipeArgument: '2017-12-05 05:30:00 for pipe 'DatePipe' - Safari
- java - 作为 Java 对象接收来自 REST 客户端的响应
- python - 约束python函数参数
- java - 在矩阵中查找元素之和为给定数字的路径