所以,我对 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)
实际上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的支持即将结束。
存在缩进错误,因为当您以固定方式编码时,您在给出空格时犯了错误,这里就是例子
def fun():
a = int(input('enter the first number'))
b = int(input('enter the second number'))
print(a+b)
fun()
这是完美的代码,但如果我们只在空间上进行这样的更改:
def fun():
a = int(input('enter the first number'))
b = int(input('enter the second number'))
print(a+b)
fun()
注意这里的打印函数,我给了它另一个可用空间,现在这段代码给了我这样的错误:
IndentationError: unexpected indent
您可以通过检查您给出的原始代码和行间距来使其正确
您在第 22 行错过了一个选项卡。在
if
语句之后,您需要将代码从 if
缩进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.")