Develop an algorithm to detect and fix Python syntax errors in a given code snippet.
def test ():
    x = 5
    y = 3 
    z
def test():
    x = 5
    y = 3
    try:
        z
    except NameError:
        print("NameError: Name 'z' is not defined")