0

When I run this I get an invalid syntax error with the "=" part of the "!=" sign highlighted

I don't think it's a syntax problem on that line per say because I tried using a "==" sign and I still got the same error with the second "=" sign being highlighted

def delete(line_number):
    src = "userinfo.txt"
    dest = "tempfile.txt"
    counter = 1
    with open(src, "r") as input:
        with open(dest, "w") as output: 
            for line in input:
                if counter++ != line_number:
                    output.write(line)
khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

3

Python doesn't support incremental operated like other languages so you need to use + to increment your count.

def delete(line_number):
    src = "userinfo.txt"
    dest = "tempfile.txt"
    counter = 1
    with open(src, "r") as input:
        with open(dest, "w") as output: 
            for line in input:
                counter += 1
                if counter != line_number:
                    output.write(line)

NOTE: counter += 1 is same as counter = counter + 1.

Shanteshwar Inde
  • 1,438
  • 4
  • 17
  • 27