1

I have this python code here:

def location(location_id, max_id=None):
        # Send http request to find location
        if (self.login_status):
            get_location = {'location_id': location_id}
            url_location_detail = self.url_location_detail % (location_id)
        try:
            get_location_id = self.i.get(url_location_detail, data=body)
            if location.status_code == 200:
               log_string = "Get location id: %d" % (location_id)
               self.write_log(log_string)

        if max_id is not None: payload['max_id'] = max_id
            try:
                res = requests.get(url, params=payload).json()
                body   = res['location']
                cursor = res['location']['media']['page_info']['end_cursor']
            except: raise
            return InstagramExploreResponse(data=body, cursor=cursor)

When I run the program, it raises the following error:

SyntaxError: invalid syntax

The error seems to show up at line 447 which is the following:

if max_id is not None: payload['max_id'] = max_id

The weird thing is that the code was working before and I added some code before this line so I think the error comes from a previous line.

Unfortunately, I can't see it.

Thanks!

justinedps26
  • 159
  • 1
  • 3
  • 15

1 Answers1

1

You can find what you need here: Putting a simple if-then statement on one line

They already explain why is this error showing and some ways to solve it

Being more clear here, you can't put what the inside the if in front of the if in python. This:

if max_id is not None: payload['max_id'] = max_id

Become this:

if max_id is not None:
    payload['max_id'] = max_id
Community
  • 1
  • 1
Xidh
  • 582
  • 1
  • 5
  • 19
  • Thanks. I've checked your link but I can't find the answer to my question. Can you raise a syntax error in my code? Because it was working find before I add some other lines and never raised the invalid syntax before. – justinedps26 Apr 06 '17 at 12:37
  • I just edited the answer. This should be easier to see now – Xidh Apr 20 '17 at 11:48