Error: unindent does not match any outer indentation level (Making a reddit bot)

Error: unindent does not match any outer indentation level (Making a reddit bot)

所以,我是 Python 的新手。我尝试制作一个 reddit 机器人,我修复了大部分错误。但是当我尝试修复它时,这个问题开始让我头疼。

print("String with \"best girl\" found in comment", str(comment.id))
IndentationError: unindent does not match any outer indentation level.

和指向代码中最后一个 ) 的箭头。经过一番研究,我发现没有任何帮助。同样,我对编程完全陌生。

我的代码:

import praw
import config
import time
import os

def bot_login():
    print "Loggin in..."
    r = praw.Reddit(username = config.username,
            password = config.password,
            client_id = config.client_id,
            client_secret = config.client_secret,
            user_agent = "busterronitest's dog comment responder v0.1")
    print "Logged in!"

    return r

def run_bot(r, comments_replied_to):
    print "Obtaining 25 comments..."

    for comment in r.subreddit('TheTempleOfOchako').comments(limit=10):
        if "best girl" or "Best girl" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
            print("String with \"best girl\" found in comment", str(comment.id))
            comment.reply("We all know ochako is best girl, [Just look at her!](https://i.imgur.com/SuKe7l0.jpg) This action was performed by a bot, contact u/Monikas_Comin if you have any issues.")
            print("Replied to comment", str(comment.id))

            comments_replied_to.append(comment.id)

            with open ("comments_replied_to.txt", "a") as f:
                f.write(comment.id + "\n")

    print "Sleeping for 10 seconds..."
    #Sleep for 10 seconds...
    time.sleep(10)

def get_saved_comments():
    if not os.path.isfile("comments_replied_to.txt"):
        comments_replied_to = []
    else:
        with open("comments_replied_to.txt", "r") as f:
            comments_replied_to = f.read()
            comments_replied_to = comments_replied_to.split("\n")
            comments_replied_to = filter(None, comments_replied_to)

    return comments_replied_to

r = bot_login()
comments_replied_to = get_saved_comments()
print comments_replied_to

while True:
    run_bot(r, comments_replied_to)

您在第 22 行错过了 (a) 个制表符。在 if 语句之后,您需要将代码从 if 缩进
缩进一级 这可能会奏效,但我不知道它是否像代码

的 objective 一样奏效
for comment in r.subreddit('TheTempleOfOchako').comments(limit=10): 
    if "best girl" or "Best girl" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me(): 
         print("String with \"best girl\" found in comment", str(comment.id))
         comment.reply("We all know ochako is best girl, [Just look at her!](https://i.imgur.com/SuKe7l0.jpg) This action was performed by a bot, contact u/Monikas_Comin if you have any issues.")

实际上 Python 给出了不恰当的错误信息。在您的代码中,您混合了空格和制表符。然而,这两个东西不应该混在 Python 中。这是修改后的代码,所有制表符都替换为四个空格:

import praw
import config
import time
import os

def bot_login():
    print "Loggin in..."
    r = praw.Reddit(username = config.username,
            password = config.password,
            client_id = config.client_id,
            client_secret = config.client_secret,
            user_agent = "busterronitest's dog comment responder v0.1")
    print "Logged in!"
    return r

def run_bot(r, comments_replied_to):
    print "Obtaining 25 comments..."
    for comment in r.subreddit('TheTempleOfOchako').comments(limit=10):
        if "best girl" or "Best girl" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
            print("String with \"best girl\" found in comment", str(comment.id))
            comment.reply("We all know ochako is best girl, [Just look at her!](https://i.imgur.com/SuKe7l0.jpg) This action was performed by a bot, contact u/Monikas_Comin if you have any issues.")
            print("Replied to comment", str(comment.id))
            comments_replied_to.append(comment.id)
            with open ("comments_replied_to.txt", "a") as f:
                f.write(comment.id + "\n")

    print "Sleeping for 10 seconds..."
    #Sleep for 10 seconds...
    time.sleep(10)

def get_saved_comments():
    if not os.path.isfile("comments_replied_to.txt"):
        comments_replied_to = []
    else:
        with open("comments_replied_to.txt", "r") as f:
            comments_replied_to = f.read()
            comments_replied_to = comments_replied_to.split("\n")
            comments_replied_to = filter(None, comments_replied_to)

    return comments_replied_to

r = bot_login()
comments_replied_to = get_saved_comments()
print comments_replied_to

while True:
    run_bot(r, comments_replied_to)

还有一个建议:您最好切换到 Python3,对 Python2 的支持即将结束。

存在缩进错误,因为当您以固定方式编码时,您在给出 space 时犯了一个错误 space 这是示例

def fun():
    a = int(input('enter the first number'))
    b = int(input('enter the second number'))
    print(a+b)
fun()

这是完美的代码,但如果我们只对 space 进行如下更改:

def fun():
    a = int(input('enter the first number'))
    b = int(input('enter the second number'))
        print(a+b)
fun()

注意这里的打印功能我已经给了它另一个免费的space现在这段代码给我的错误如下:

IndentationError: unexpected indent

您可以通过检查原始代码和您提供的 space 行来使其正确