【问题标题】:Python3 IndentationError: expected an indented block [duplicate]Python3 IndentationError:预期缩进块[重复]
【发布时间】:2021-07-12 23:56:52
【问题描述】:

我的代码不起作用,我尝试了很多东西。 我得到这个错误:IndentationError:期望一个缩进块 我希望有一个人可以帮助我。谢谢。

import requests
import threading

url = 'https://example.com'

def do_request():
while True:
response = requests.get(url)
print(response)

    threads = []

    for i in range(50):
        t = threading.Thread(target=do_request)
        t.daemon = True
        threads.append(t)

    for i in range(50):
        threads[i].start()

    for i in range(50):
        threads[i].join()

【问题讨论】:

  • 您有一个 while 循环,其中代码没有缩进。还有一个代码没有缩进的 def。这是非常基本的 Python 语法。也许您可以从教程中受益。
  • 请阅读How to Ask。始终包含 full 错误消息,而不仅仅是您认为重要的部分。您的错误消息还将包括行号、生成错误的代码和回溯。
  • 但是您的defwhile 都缺少缩进代码块。

标签: python python-3.x pip


【解决方案1】:

好吧,我相信您的问题相当简单,因为您忘记了按照错误告诉您的操作:没有正确缩进。正确缩进的代码:

import requests
import threading

url = 'https://example.com'

def do_request():
    while True:
        response = requests.get(url)
        print(response)

threads = []
for i in range(50):
    t = threading.Thread(target=do_request)
    t.daemon = True
    threads.append(t)

for i in range(50):
    threads[i].start()

for i in range(50):
    threads[i].join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多