缩进错误:需要缩进块Python 3?

缩进错误:需要缩进块Python 3?,python,python-3.x,Python,Python 3.x,您好,我对python世界非常陌生,我使用anacoda在jupyter中运行了一些示例代码,我正在尝试执行一个简单的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(qu

您好,我对python世界非常陌生,我使用anacoda在jupyter中运行了一些示例代码,我正在尝试执行一个简单的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"))

它确切地说明了它的意思:您开始定义一个函数,但没有缩进应该是该函数一部分的行(Python不知道哪些行应该是该函数的一部分,但它知道它至少应该有一行)。在任何以
结尾的行之后,都应该缩进(标准是四个空格)并保持缩进,直到您希望完成该块(此时您需要缩进)。您需要缩进函数和
if
块的内容:

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是一种对空格敏感的语言:如果在示例代码中复制而不保留缩进,它将失败。

您需要缩进
返回值和
打印值:

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"))
一般来说,在
for
之后,如果
或函数定义,则需要缩进块

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

python中需要缩进。请参考这些。

缩进在Python中的含义。你不可能把所有内容都写在左边空白处,你必须缩进函数/类定义、循环等的主体,因为缩进的结尾是告诉Python主体在哪里结束的唯一方式。这可以通过随机搜索Python中几乎所有的示例代码并注意它是如何缩进的来解决。在询问之前,请阅读错误信息并使用搜索引擎。