【问题标题】:How to use <=, >=, < and > on a variable stored in a json - python, discord.py如何在存储在 json 中的变量上使用 <=、>=、< 和 > - python,discord.py
【发布时间】:2021-03-19 07:44:53
【问题描述】:

我有一些不和谐机器人的代码,它基本上检查存储在 json 中的地精 hp,如果它没有死,它会重播战斗序列。如果它死了(json 变量小于 0)它应该停止序列。但事实并非如此。我检查了json,变量为-9,代码仍在循环。代码如下:

@client.command(name='fight_01')
@commands.cooldown(1, 30, commands.BucketType.user)
async def fight_goblin(ctx):
    users = await get_bank_data()
    user = ctx.author
    rng1 = random.randint(1, 3)
    hp = users[str(user.id)]['hp']
    users[str(user.id)]['goblin_hp_json'] = 5
    with open("mainbank.json", 'w') as f:
      json.dump(users, f)
    rng2 = random.randint(1, 3)
    if rng1 == int('2'):
        await ctx.channel.send("You searched all around and couldn't find a goblin, try again next time :(")

    elif hp <= 0:
      await ctx.channel.send("You can't go adventuring with no hp you idiot")
        
            
   


     #Define this as async then remove the set hp to 5.
     #THen call it using the loop that the stackoverflow guy gave
    else:

      while True: #an infinite loop
        goblin_hp = users[str(user.id)]['goblin_hp_json']
        if users[str(user.id)]['hp'] <= 0:
          time.sleep(1.5)
          await ctx.channel.send('You died, better luck next time')
                    #If your command stop here you can use return, else use break to get out of the loop, here I will use break
          break
             
                  
    
        if goblin_hp <= 0:
          content3 = 'You did ' + rng5 + ' damage so you killed the goblin, well done'
          await ctx.channel.send(content3)
          rng6 = random.randint(20, 100)
          users[str(user.id)]['bank'] += rng6
          await asyncio.sleep(0.8) #use await asyncio.sleep(0.8) here rather
          content4 = 'You gained ' + str(rng6) + ' coins for defeating the goblin'
                        
          await ctx.channel.send(content4)
          with open("mainbank.json", 'w') as f:
            json.dump(users, f)
                    #Same here, I will use break
          break
        else:
          await goblin_fight_sequence(ctx)
                   #(This is where I want it to return to the first else: statement)
                   #Here you can use continue to repeat your loop, or just delete this part (an you will return to the first if). To match with your current code, I will use continue
                   #continue



async def goblin_fight_sequence(ctx):
  await ctx.channel.send('You encountered a wild goblin!')
        
  await asyncio.sleep(1.5) #use await asyncio.sleep(1.5) here rather
  rng3 = random.randint(1, 10)
  if rng3 == int('3') or int('4') or int('7'):
            
    user = ctx.author
    users = await get_bank_data()
    await ctx.channel.send('The goblin got the first hit and you lost 1hp')      

                
    users[str(user.id)]['hp'] -= int('1')
    dmg = users[str(user.id)]['max_damage'] #You forgot an ] here so I replaced it
    rng4 = random.randint(0, dmg)
    rng5 = str(rng4)
    await asyncio.sleep(1.5) #use await asyncio.sleep(1.5) here rather
    content1 = 'You did ' + rng5 + ' damage'
    users[str(user.id)]['goblin_hp_json'] -= int(rng4)
    with open("mainbank.json", 'w') as f:
      json.dump(users, f)
    users = await get_bank_data()
    await ctx.channel.send(content1)

我该如何解决这个问题?

【问题讨论】:

  • 你的问题不是很清楚,能否在你的代码中添加goblin_fight_sequence函数,因为我看不到任何表明goblin_hp 被修改的东西
  • 我已经添加了代码@Baptiste
  • 老实说,我没有足够的信息来做出一个好的答案......你必须输入输出(带有打印),如果有的话,或者其他可以帮助的东西一个解决方案...
  • 没有错误,代码从未停止循环,这意味着它总是进入“else”语句。这意味着我猜 if 不起作用
  • 这很奇怪,因为您明确地将用户设置为 5,它应该是一个 int。但是,在解析 json 时,您有时可能会得到 str 而不是 int。因此,您可能希望在某个时候将变量强制转换为 int。这可以通过print(goblin_hp, type(goblin_hp)) 进行验证。不然我真不知道

标签: python json discord.py


【解决方案1】:

我设法通过添加 users = await get_bank_data() 来解决这个问题,这实际上将 JSON 打开为已读。我在 while true 下添加了它。老实说,我不完全确定这是如何解决的,但确实如此。

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多