为什么这个命令运行得很好,但当我运行它时,它允许你猜测两次?(Discord.py)



基本上该命令运行两次,但只有一个版本如果有人知道它为什么运行两次,你能解释一下吗,因为这个错误似乎让我感兴趣

@bot.command()
async def numguess(ctx):
primary_id = str(ctx.message.author.id)
number = random.randint(0, 40)
await ctx.send("I am thinking of a number from 1-40. What is it?")
for i in range(0, 50):
response = await ctx.bot.wait_for('message')
guess = int(response.content)
if guess > number:
await ctx.send('That guess is too big! Try again!')
elif guess < number:
await ctx.send('That guess is too small! Try again!')
else:
await ctx.send(f'You got it right! It only took you {i + 1} attempts! {100 - (i+1)} coins have been earned!')
amount = 100 - (i + 1)
amounts[primary_id] += amount
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)```

我发现,如果最后我让它生成一个新的数字,它仍然会运行两次,但数字会不同,这意味着你不能永远种钱

@bot.command()
async def numguess(ctx):
primary_id = str(ctx.message.author.id)
number = random.randint(0, 40)
await ctx.send("I am thinking of a number from 1-40. What is it?")
for i in range(0, 50):
response = await ctx.bot.wait_for('message')
guess = int(response.content)
if guess > number:
await ctx.send('That guess is too big! Try again!')
elif guess < number:
await ctx.send('That guess is too small! Try again!')
else:
await ctx.send(f'You got it right! It only took you {i + 1} attempts! {100 - (i+1)} coins have been earned!')
number = random.randint(0, 40)
amount = 100 - (i + 1)
amounts[primary_id] += amount
_save()
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)```

相关内容

最新更新