标识中的Python语法错误?

时间:2016-01-23 19:15:28

标签: python

我是Python的新手,我正在尝试理解一些代码。 在下面的代码中有一个语法错误,但我真的不知道是什么。

def parse(data):
print(data)

def get_set(json_url):
url = json_url
response = urllib.urlopen(url)
data = json.loads(response.read())

s_code = '0'

print data
s_code = data["statusCode"]
print s_code

    seconds_waiting = 10
       if s_code == 200:
    url = json_url
               response = urllib.urlopen(url)
               data = json.loads(response.read())

            parse(data)
elif s_code != 200:

    print "waiting " + str(seconds_waiting) + " second(s)..."
               time.sleep(seconds_waiting)
            get_set(json_url)

get_set(json_url):

3 个答案:

答案 0 :(得分:5)

大多数问题只是代码格式不正确(缩进用于在Python中定义方法和条件语句的主体)

这是代码

def parse(data):
    print(data)


def get_set(json_url):
    url = json_url
    response = urllib.urlopen(url)
    data = json.loads(response.read())

    s_code = '0'

    print data
    s_code = data["statusCode"]
    print s_code

    seconds_waiting = 10
    if s_code == 200:
        url = json_url
        response = urllib.urlopen(url)
        data = json.loads(response.read())

        parse(data)
    elif s_code != 200:
        print "waiting " + str(seconds_waiting) + " second(s)..."
        time.sleep(seconds_waiting)
        get_set(json_url)

答案 1 :(得分:3)

更正(?):

def parse(data):
    print(data)

def get_set(json_url):
    url = json_url
    response = urllib.urlopen(url)
    data = json.loads(response.read())

    s_code = '0'

    print data
    s_code = data["statusCode"]
    print s_code

    seconds_waiting = 10
    if s_code == 200:
        url = json_url
        response = urllib.urlopen(url)
        data = json.loads(response.read())
        parse(data)
    elif s_code != 200:
        print "waiting " + str(seconds_waiting) + " second(s)..."
        time.sleep(seconds_waiting)
        get_set(json_url)

缩进是Python中的主要功能/要求之一。如果上面的代码在一个类中,则需要再次缩进。但正如评论中指出的那样,请read the official tutorial(再次)。

答案 2 :(得分:3)

由于存在大量缩进错误,我建议您采用一种可能的解决方案:

def parse(data):
    print(data)

def get_set(json_url):
    url = json_url
    response = urllib.urlopen(url)
    data = json.loads(response.read())
    s_code = '0'
    print data
    s_code = data["statusCode"]
    print s_code
    seconds_waiting = 10
    if s_code == 200:
        url = json_url
        response = urllib.urlopen(url)
        data = json.loads(response.read())
        parse(data)
    elif s_code != 200:
        print "waiting " + str(seconds_waiting) + " second(s)..."
        time.sleep(seconds_waiting)
        get_set(json_url)

P.S。我没有检查程序的逻辑(仅修复缩进)