-4

Hi i am very new to python world, i have ran some sample codes in jupyter using anacoda, i am trying to execute one simple request code of python

import requests

def query_raw(text, url="https://mywebsite.com/plain"):
return requests.post(url, data={'sample_text': text}).json()

if __name__ == '__main__':
print(query_raw("Give me the list of users"))

enter image description here

Gopi Lal
  • 417
  • 5
  • 23
  • Indentation *means something* in Python. You cannot write everything flush with the left margin like that, you have to indent the body of function/class definitions, loops, etc., as the end of the indention is your *only* way of telling Python where that body ends. – jasonharper Jul 23 '20 at 13:13
  • This could be solved by just randomly googling almost any example code in Python and paying attention to how it is indented. Please read error messages and use search engines before asking. – pavelsaman Jul 23 '20 at 13:15

3 Answers3

2

It says exactly what it means: You began defining a function, but didn't indent the lines that are supposed to be a part of that function (Python doesn't know which lines should be part of it, but it knows it should have at least one line). After any line ending in :, you're supposed to indent (standard is four spaces) and keep indenting until you want that block to be done (at which point you dedent). You need to indent the contents of both the function and the if block:

def query_raw(text, url="https://mywebsite.com/plain"):
    return requests.post(url, data={'sample_text': text}).json()

if __name__ == '__main__':
    print(query_raw("Give me the list of users"))

Python is a whitespace sensitive language: If you copy in sample code without preserving the indentation, it will fail.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

you need to indent the return and the print:

import requests

def query_raw(text, url="https://mywebsite.com/plain"):
    return requests.post(url, data={'sample_text': text}).json()

if __name__ == '__main__':
    print(query_raw("Give me the list of users"))

genereally speaking after a for a if or a function definition, you need an indented block.

RO-ito
  • 111
  • 5
1
import requests

def query_raw(text,     url="https://mywebsite.com/plain"):
  return requests.post(url, data={'sample_text': text}).json()

if __name__ == '__main__':
  print(query_raw("Give me the list of users"))

Indentation is required in python. Refer to these notes.

Jain
  • 75
  • 7