pythonpraw

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


So, I'm new to Python in all. I tried my hand at making a reddit bot, I fixed most errors. But this one kinda started beating my head in as I tried fixing it.

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

And an arrow pointing to the last ) in the code. After researching a bit, I found no help. Again, I'm completely new to programming in all.

My Code:

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)

Solution

  • Actually Python gave the inappropriate error message. In your code you are mixing spaces and tabs. However these two things should not be mixed in Python. Here is the modified code, with all tabs replaced by four spaces:

    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)
    

    And one suggestion: you'd better switch to Python3, the support to Python2 is ending soon.