簡體   English   中英

需要一個縮進塊,python

[英]Expected an indented block, python

您好,有人可以解決它嗎?我試圖在網上找到解決方案,但沒有任何效果。

Python 版本:3.7.9

if engine == "example1":
        search = example1(engine)
        request = partial(search.search_for, string)
        all = p.map(request, pages)
            
elif engine == "example2":
        filepath = "list.txt"
        with open(filepath) as fp:
            line = fp.readline()
            count = 1
            while line:
                search = example2(engine)
                request = partial(search.search_for, line.strip())
                all = p.map(request, pages)
                line = fp.readline()
                count += 1
            input(" press close to exit ")

似乎最大的問題是緊跟在您的with語句之后的行。 這些應該縮進成為上下文管理器 scope 的一部分。

elif engine == "example2":
    filepath = "list.txt"
    with open(filepath) as fp:
        line = fp.readline() # <--- note indent
        count = 1            # <--- note indent
        while line:
            search = example2(engine)
            request = partial(search.search_for, line.strip())
            all = p.map(request, pages)
            line = fp.readline()
            count += 1

這里有 2 個意圖錯誤。復制此代碼並嘗試

if engine == "example1":
     search = example1(engine)
     request = partial(search.search_for, string)
     all = p.map(request, pages)

1.在elif中,行前有一個空格

elif engine == "example2":
    filepath = "list.txt"

2.在下一行打開后需要留4個空格

    with open(filepath) as fp:
        line = fp.readline()
    count = 1
    while line:
        search = example2(engine)
        request = partial(search.search_for, line.strip())
        all = p.map(request, pages)
        line = fp.readline()
        count += 1
    input(" press close to exit ")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM