简单的打印语句在 python 中的 if 语句的方法中不起作用

我最近开始学习 Python 代码,自从过去 4 天以来,一个简单的打印语句给我带来了麻烦。


问题:打印语句在validatePostcode(postcode)if 语句的方法中不起作用。分配的值是 200(状态代码),它在没有 if 语句的情况下打印得很好。此外,当我与该 API 的 True(结果值)进行比较时,它在没有 if 语句的情况下工作正常,为什么在我应用 if 并尝试比较后它不起作用?


错误:


  File "./py_script3.py", line 32

    print ("Congrats")

        ^

IndentationError: expected an indented block

    #!/usr/bin/env python3



    import os,re,sys



    import urllib.request as req

    import json


    def loadJsonResponse(url):

        #return json.loads(req.urlopen(url).read().decode('utf-8'))['result']

        #return json.loads(req.urlopen(url).read().decode('utf-8'))['status']

        print ("I am in loadJsonResponse before returning string")

        string = json.loads(req.urlopen(url).read().decode('utf-8'))

        return string

        print ("I am in loadJsonResponse after returning string")


    def lookuppostcode(postcode):

        url = 'https://api.postcodes.io/postcodes/{}'.format(postcode)

        return loadJsonResponse(url)


    def validatePostcode(postcode):

        url = 'https://api.postcodes.io/postcodes/{}/validate'.format(postcode)

        #return loadJsonResponse(url)

        string = json.loads(req.urlopen(url).read().decode('utf-8'))

        Value = str(string['status'])

        print (Value)

        if Value == 200 :

        print ("Congrats")


    def randomPostcode():

        url = 'https://api.postcodes.io/random/postcodes'

        return loadJsonResponse(url)


    def queryPostcode(postcode):

        url = 'https://api.postcodes.io/postcodes?q={}'.format(postcode)

        return loadJsonResponse(url)


    def getAutoCompletePostcode(postcode):

        url = 'https://api.postcodes.io/postcodes/{}/autocomplete'.format(postcode)

        return loadJsonResponse(url)


    #Input = input("Enter the postcode : ")

    #print(lookuppostcode('CB3 0FA'))

    validatePostcode('CB3 0FA')

    #print(queryPostcode('HU88BT'))

    #print(randomPostcode(Input))



吃鸡游戏
浏览 293回答 3
3回答

慕村225694

这段代码(产生错误):if Value == 200 : print ("Congrats")应该if Value == 200 :     print ("Congrats")因为python在条件之后需要一个缩进的块,就像消息错误对你说的一样

烙印99

您应该像这样缩进打印语句:if Value == 200 :    print ("Congrats")您可以在此处阅读更多相关信息!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python