SyntaxError: default ‘except:’ must be last

I copied and pasted the first half (the variables with the 1 on the end) and pasted it underneath but changed 1 to 2. The first half (the 1) works, but right at the end of the second one it gives me that syntax error. I have researched around, and most problems was because they had too many except s and stuff, but I am pretty certain that all my try s and except s are right.

My code:

  try:
    headers = {'content-type': 'application/json; charset=UTF-8'}
    url = 'https://surviv.io/api/user_stats'
    payload = {"slug": name1, "interval": interval, "mapIdFilter": gamemode}
    r = requests.post(url=url, headers=headers, data=json.dumps(payload))
    c = r.json()
    kills1 = str(c["kills"])
    wins1 = str(c["wins"])
    games1 = str(c["games"])
    kg1 = str(c["kpg"])
    mostkills1 = str(max([i["mostKills"] for i in c["modes"]]))
    maxdamage1 = str(max([i["mostDamage"] for i in c["modes"]]))
    username1 = str(c["username"])
    try:
      sologames1 = str(c['mode'][0]['games'])
      sologwins1 = str(c['mode'][0]['wins'])
      solokills1 = str(c['mode'][0]['kills'])
      solowinrate1 = str(c['mode'][0]['winPct'])
      solomostkills1 = str(c['mode'][0]['mostKills'])
      solomostdamage1 = str(c['mode'][0]['mostDamge'])
      solokpg1 = str(c['mode'][0]['kpg'])
      soloavgTA1 = str(c['mode'][0]['avgTimeAlive'])
      solo1 = True
    except:
      solo1 = False
    try:
      duogames1 = str(c['mode'][1]['games'])
      duogwins1 = str(c['mode'][1]['wins'])
      duokills1 = str(c['mode'][1]['kills'])
      duowinrate1 = str(c['mode'][1]['winPct'])
      duomostkills1 = str(c['mode'][1]['mostKills'])
      duomostdamage1 = str(c['mode'][1]['mostDamge'])
      duokpg1 = str(c['mode'][1]['kpg'])
      duoavgTA1 = str(c['mode'][1]['avgTimeAlive'])
      duo1 = True
    except:
      duo1 = False
    try:
      squadgames1 = str(c['mode'][0]['games'])
      squadwins1 = str(c['mode'][0]['wins'])
      squadkills1 = str(c['mode'][0]['kills'])
      squadwinrate1 = str(c['mode'][0]['winPct'])
      squadmostkills1 = str(c['mode'][0]['mostKills'])
      squadmostdamage1 = str(c['mode'][0]['mostDamge'])
      squadkpg1 = str(c['mode'][0]['kpg'])
      squadavgTA1 = str(c['mode'][0]['avgTimeAlive'])
      squad1 = True
    except:
      squad1 = False
  except:
    try:
      if gamemode != "-1":
        headers = {'content-type': 'application/json; charset=UTF-8'}
        url = 'https://surviv.io/api/user_stats'
        payload = {"slug": name1, "interval": "all", "mapIdFilter": gamemode}
        r = requests.post(url=url, headers=headers, data=json.dumps(payload))
        c = r.json()
        kills1 = str(c["kills"])
        wins1 = str(c["wins"])
        games1 = str(c["games"])
        kg1 = str(c["kpg"])
        mostkills1 = str(max([i["mostKills"] for i in c["modes"]]))
        maxdamage1 = str(max([i["mostDamage"] for i in c["modes"]]))        
        gamemode1 = True
      else:
        gamemode1 = False
    except:
      try:
        if gamemode == "-1" and interval != "all":
          headers = {'content-type': 'application/json; charset=UTF-8'}
          url = 'https://surviv.io/api/user_stats'
          payload = {"slug": name1, "interval": "all", "mapIdFilter": "-1"}
          r = requests.post(url=url, headers=headers, data=json.dumps(payload))
          c = r.json()
          kills1 = str(c["kills"])
          wins1 = str(c["wins"])
          games1 = str(c["games"])
          kg1 = str(c["kpg"])
          mostkills1 = str(max([i["mostKills"] for i in c["modes"]]))
          maxdamage1 = str(max([i["mostDamage"] for i in c["modes"]]))
          timeperiod1 = True
        else:
          timeperiod1 = False
      except:
        print("That player does not exist")
  try:
    headers = {'content-type': 'application/json; charset=UTF-8'}
    url = 'https://surviv.io/api/user_stats'
    payload = {"slug": name2, "interval": interval, "mapIdFilter": gamemode}
    r = requests.post(url=url, headers=headers, data=json.dumps(payload))
    c = r.json()
    kills2 = str(c["kills"])
    wins2 = str(c["wins"])
    games2 = str(c["games"])
    kg2 = str(c["kpg"])
    mostkills2 = str(max([i["mostKills"] for i in c["modes"]]))
    maxdamage2 = str(max([i["mostDamage"] for i in c["modes"]]))
    username2 = str(c["username"])
    try:
      sologames2 = str(c['mode'][0]['games'])
      sologwins2 = str(c['mode'][0]['wins'])
      solokills2 = str(c['mode'][0]['kills'])
      solowinrate2 = str(c['mode'][0]['winPct'])
      solomostkills2 = str(c['mode'][0]['mostKills'])
      solomostdamage2 = str(c['mode'][0]['mostDamge'])
      solokpg2 = str(c['mode'][0]['kpg'])
      soloavgTA2 = str(c['mode'][0]['avgTimeAlive'])
      solo2 = True
    except:
      solo2 = False
    try:
      duogames2 = str(c['mode'][1]['games'])
      duogwins2 = str(c['mode'][1]['wins'])
      duokills2 = str(c['mode'][1]['kills'])
      duowinrate2 = str(c['mode'][1]['winPct'])
      duomostkills2 = str(c['mode'][1]['mostKills'])
      duomostdamage2 = str(c['mode'][1]['mostDamge'])
      duokpg2 = str(c['mode'][1]['kpg'])
      duoavgTA2 = str(c['mode'][1]['avgTimeAlive'])
      duo2 = True
    except:
      duo2 = False
    try:
      squadgames2 = str(c['mode'][0]['games'])
      squadwins2 = str(c['mode'][0]['wins'])
      squadkills2 = str(c['mode'][0]['kills'])
      squadwinrate2 = str(c['mode'][0]['winPct'])
      squadmostkills2 = str(c['mode'][0]['mostKills'])
      squadmostdamage2 = str(c['mode'][0]['mostDamge'])
      squadkpg2 = str(c['mode'][0]['kpg'])
      squadavgTA2 = str(c['mode'][0]['avgTimeAlive'])
      squad2 = True
    except:
      squad2 = False
  except:
    try:
      if gamemode != "-1":
        headers = {'content-type': 'application/json; charset=UTF-8'}
        url = 'https://surviv.io/api/user_stats'
        payload = {"slug": name2, "interval": "all", "mapIdFilter": gamemode}
        r = requests.post(url=url, headers=headers, data=json.dumps(payload))
        c = r.json()
        kills2 = str(c["kills"])
        wins2 = str(c["wins"])
        games2 = str(c["games"])
        kg2 = str(c["kpg"])
        mostkills2 = str(max([i["mostKills"] for i in c["modes"]]))
        maxdamage2 = str(max([i["mostDamage"] for i in c["modes"]]))        
        gamemode2 = True
      else:
        gamemode2 = False
    except:
      try:
        if gamemode == "-1" and interval != "all":
          headers = {'content-type': 'application/json; charset=UTF-8'}
          url = 'https://surviv.io/api/user_stats'
          payload = {"slug": name2, "interval": "all", "mapIdFilter": "-1"}
          r = requests.post(url=url, headers=headers, data=json.dumps(payload))
          c = r.json()
          kills2 = str(c["kills"])
          wins2 = str(c["wins"])
          games2 = str(c["games"])
          kg2 = str(c["kpg"])
          mostkills2 = str(max([i["mostKills"] for i in c["modes"]]))
          maxdamage2 = str(max([i["mostDamage"] for i in c["modes"]]))
          timeperiod2 = True
        else:
          timeperiod2 = False
      except:
        print("That player does not exist")


Read more here: https://stackoverflow.com/questions/68463137/syntaxerror-default-except-must-be-last

Content Attribution

This content was originally published by Wolfmann Games at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: