消除嵌套for循环中的重复响应



我正在为我的不和谐机器人寻找解决方案,它是用Py编写的,它被编码为在游戏数据库api中搜索特定的玩家名称,如果发现,它会发送一条关于服务器ID不和谐的消息,这部分工作,但是现在我想添加一条消息,如果玩家在搜索所有数据库后没有找到,这里是代码:

@client.command()
async def whereis(ctx, player):  # General stats
for element in gameIDs["IDs"]:
url0 = apiLink1 + "/players/?gameId=" + element
r = requests.get(url0)
rs = r.json()
a = rs['teams'][0]['players']
b = rs['teams'][1]['players']
c = a + b
d = rs['serverinfo']['name']
for element1 in c:
if element1["name"] == str(player):
embed = discord.Embed(title=player + " HAS BEEN FOUND",description=d)
await ctx.send(embed=embed)
break
else:
print("not found")

第一个for循环是检查每个数据库id,第二个for循环是验证每个数据库与给定的球员,我目前的解决方案返回"未找到"对于每个gameID实例,我理解为什么,但我只希望它在gameID列表耗尽时发送消息。我该怎么做呢?

提前感谢大家。

如果没有找到,只是添加一个player_foundbool和打印呢?

async def whereis(ctx, player):  # General stats
player_found = False
for element in gameIDs["IDs"]:
url0 = apiLink1 + "/players/?gameId=" + element
r = requests.get(url0)
rs = r.json()
a = rs['teams'][0]['players']
b = rs['teams'][1]['players']
c = a + b
d = rs['serverinfo']['name']
for element1 in c:
if element1["name"] == str(player):
player_found = True
embed = discord.Embed(title=player + " HAS BEEN FOUND",description=d)
await ctx.send(embed=embed)
break
if not player_found:
print("not found")

相关内容

  • 没有找到相关文章

最新更新