-3

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)
Trooper Z
  • 1,617
  • 14
  • 31

3 Answers3

2

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.

Mia
  • 2,466
  • 22
  • 38
  • @CipherTheUniverse If my answer solves your issue, please consider to accept/upvote the answer. – Mia Aug 11 '18 at 03:41
1

the indentation error exists because when you code in a fixed manner there you make a mistake in giving a space here is the example

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

this is perfect code but if we only made change in space like this:

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

notice here the print function I have given it another free space now this code is giving me error like:

IndentationError: unexpected indent

you can make it correct by checking the original code and line spaces that you have given

VIKAS RATHEE
  • 97
  • 2
  • 13
0

You miss (a) tab(s) at line 22. After an if statement, you need to indent your code one level from your if indent
This might work, though I don't know if it work as your objective of the code

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.")
enamoria
  • 896
  • 2
  • 11
  • 29
  • Now it's saying the same error to this bit of code. Specifically the colon. `if "best girl" or "Best girl" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():` – Cipher The Universe Aug 11 '18 at 03:37
  • Maybe tabs and spaces are mixed. Indent in python should be only either tabs or spaces. Check for your indent and tell me if you still have problem – enamoria Aug 11 '18 at 03:38