简体   繁体   中英

simple print statement is not working inside method with if statement in python

I started learning Python code recently and one simple print statement is giving me trouble since last 4 days.

Problem: the print statement is not working inside the validatePostcode(postcode) method for if-statement. The assigned value is 200 (status code) which is printing fine without the if-statement. Also, when I compare with the True (result value) for that API it works fine without if-statement, why it is not working after I apply the if and try to compare?

Error:

  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))

You should indent the print statement like so:

if Value == 200 :
   print ("Congrats")

You can read more about this here !

From https://docs.python.org/2.0/ref/indentation.html :

Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

By doing

if Value == 200:
print ("Congrats")

Python interprets the two lines as two different groups of statements. What you should do is:

if Value == 200:
    print ("Congrats")

This piece of code (which is generating the error):

if Value == 200 : 
print ("Congrats")

Should be

if Value == 200 : 
    print ("Congrats")

Because python expects an indented block after the conditional, just like the message error is saying to you

Need to add an indent after the if statement. You can do so by pressing return after typing the colon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM