-2

I'm trying to look through a long list of json objects in a for loop but when my if statement is called my else statement still runs.

Code

import requests
command = message.content.lower()
command = re.sub("!stock ", "", command)
r = requests.get(api)
response = r.json()

for line in response['data']['stock']:
  if line['country_name'].lower() == command:
  specific = discord.Embed(title="Cloaked Stock Count", description="Stock count for `" + command + "`", timestamp=datetime.now(),
                    color=0xCF22FF, url="https://cloaked.cc/")
  specific.add_field(name=line['country_name'] + " (" + line['iso_code'] + ")", value= "Stock: " + str(line['invite_total']))
  await message.author.send("Stock for `" + command + "`", embed=specific)

  elif line['iso_code'].lower() == command:
  specific = discord.Embed(title="Cloaked Stock Count",
                          description="Stock count for `" + command + "`",
                          timestamp=datetime.now(),
                          color=0xCF22FF, url="https://cloaked.cc/")
  specific.add_field(name=line['country_name'] + " (" + line['iso_code'] + ")",
                    value="Stock: " + str(line['invite_total']))
  await message.author.send("Stock for `" + command + "`", embed=specific)

  else:
  await message.channel.send("Didn't find")

API response

{
"data":{      "message":"Stock request was successful",
      "exec_time":0.0018777847290039062,
      "stock":[         {
            "iso_code":"DE",
            "country_name":"Germany",
            "invite_total":0
         
},
         {
            "iso_code":"US",
            "country_name":"United States",
            "invite_total":8
         
},
         {
            "iso_code":"GB",
            "country_name":"United Kingdom",
            "invite_total":0
         
},
         {
            "iso_code":"NO",
            "country_name":"Norway",
            "invite_total":0
         
},
         {
            "iso_code":"CA",
            "country_name":"Canada",
            "invite_total":0
         
},
         {
            "iso_code":"AR",
            "country_name":"Argentina",
            "invite_total":3
         
},
         {
            "iso_code":"BR",
            "country_name":"Brazil",
            "invite_total":0
         
},
         {
            "iso_code":"SK",
            "country_name":"Slovakia",
            "invite_total":0
         
},
         {
            "iso_code":"MX",
            "country_name":"Mexico",
            "invite_total":0
         
},
         {
            "iso_code":"ID",
            "country_name":"Indonesia",
            "invite_total":2
         
},
         {
            "iso_code":"SE",
            "country_name":"Sweden",
            "invite_total":0
         
},
         {
            "iso_code":"TR",
            "country_name":"Turkey",
            "invite_total":0
         
},
         {
            "iso_code":"AU",
            "country_name":"Australia",
            "invite_total":0
         
},
         {
            "iso_code":"EC",
            "country_name":"Ecuador",
            "invite_total":3
         
},
         {
            "iso_code":"DK",
            "country_name":"Denmark",
            "invite_total":0
         
},
         {
            "iso_code":"CH",
            "country_name":"Switzerland",
            "invite_total":0
         
},
         {
            "iso_code":"CZ",
            "country_name":"Czech Republic",
            "invite_total":0
         
},
         {
            "iso_code":"CL",
            "country_name":"Chile",
            "invite_total":0
         
   }
 }
}

When command is found in line['country_name'] my else statement still runs and it spams the await message.channel.send("Didn't find")

edit: I tried the same thing with an elif chain and I got the same results

fiji
  • 228
  • 5
  • 21

1 Answers1

0

If you have a bunch of exclusive if statements, instead of playing with elif, I'd recommend to use continue, if you are sure your if statements are fully exclusive, that is, if one runs, the rest is not needed:

for line in response['data']['stock']:
    if line['country_name'].lower() == command:
        # your code
        continue

    if line['iso_code'].lower() == command:
        # your code
        continue

    await message.channel.send("Didn't find")
lenik
  • 23,228
  • 4
  • 34
  • 43