Invalid Syntax while try to know if Is valid contain Yes in json

Below is my code

  if final["NetworkName"]:

    
   if final["IsValid" == "Yes"]:
    print(f'{gr}{final["PhoneNumber"]}{res}{yl} => {cy}{final["NetworkName"]}{res}')
    save = open(f'Result/{final["NetworkName"]}.txt', "a+")
    save.write(str(i) + "\n")
    else: print(f'{yl}{final["PhoneNumber"]}{res}{yl} => {cy}{final["NetworkName"]}{res}')
    
else:
    print(f'{red}{final["PhoneNumber"]} => Die{res}')
    dk = open("Result/die.txt", "a+")
    dk.write(str(i) + "\n")

maybe,

if final["IsValid"] == "Yes":
    ...

or could use,

str.__eq__(final["IsValid"], "Yes")

or could use map

list(map(str.__eq__, [final["IsValid"]], ["Yes"]))

or starmap

from itertools import starmap
list(starmap(str.__eq__, [(final["IsValid"], "Yes")]))

I think match-case should also work,

match final["IsValid"]:
    case "Yes":
        ...

Okay let me try it out

Well, I’m still getting syntax error, I’m new to python though, what i notice is that, i can’t use if else maybe i was wrong with my code

The if and the else: must have the same number spaces indent.
Your code does not,

Number space, you mean the both should be inline like straight from

      if
      :
      :
      else

No, indentation marks blocks of code in Python. Statements in the same block must start at the same indentation level. Some statements (like if) must be followed by a new block of code (i.e. with higher indentation).[1] In addition the first line of code must not be indented.

if condition:
    pass
    pass
    if condition2:
        pass
else:  # else branch of the first if statement
    pass
pass  # continuation of the top-level block of code

You should look into some tutorials. For example:


  1. If they are not followed by just a single command on the same line. ↩︎