Hack Forums
[Python] Why does this code doesn't work ? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Why does this code doesn't work ? (/showthread.php?tid=5946887)



Why does this code doesn't work ? - kookur - Mar 3, 2019

Code
import praw

# #---# Variables #---# #
client_id = ""
client_secret = ""
password = ""
user_agent = ""
username = ""
message_to_reply = "Thank you for the message."
# #-------------------# #

reddit = praw.Reddit(client_id=client_id, client_secret=client_secret,
                     password=password, user_agent=user_agent,
                     username=username)

print("Started.")

while True:
    for message in reddit.inbox.unread(limit=None):
        message.reply(message_to_reply)
        message.mark_read()


What i should edit except account username and password for this code to work and reply on the messages ? What is wrong with this code ?


RE: Why does this code doesn't work ? - Nachfolger - Mar 3, 2019

(Mar 3, 2019 09:24 AM)kookur Wrote:  
Code
import praw

# #---# Variables #---# #
client_id = ""
client_secret = ""
password = ""
user_agent = ""
username = ""
message_to_reply = "Thank you for the message."
# #-------------------# #

reddit = praw.Reddit(client_id=client_id, client_secret=client_secret,
                    password=password, user_agent=user_agent,
                    username=username)

print("Started.")

while True:
   for message in reddit.inbox.unread(limit=None):
       message.reply(message_to_reply)
       message.mark_read()


What i should edit except account username and password for this code to work and reply on the messages ?  What is wrong with this code ?
I have very limited knowledge with Python, but why are you using a continuous loop (while True) and then followed by for loop? Are you trying to keep this code running forever?


RE: Why does this code doesn't work ? - Socks. - Mar 3, 2019

Add reddit.inbox.mark_read(unread_messages) outside the for loop then a conditional statement stating that if all messages are marked as read, break the while loop as Nachfolger stated


RE: Why does this code doesn't work ? - lockhead - Mar 4, 2019

(Mar 3, 2019 11:41 AM)Nachfolger Wrote:  I have very limited knowledge with Python, but why are you using a continuous loop (while True) and then followed by for loop? Are you trying to keep this code running forever?

I think he might want to keep it running in the background to automatically respond to incoming messages. Not sure tho.

@OP: Can you explain what the code is suppose to do and more importantly what exactly isn't working? If you don't want the program to keep running forever, get rid of the while loop entirely.


RE: Why does this code doesn't work ? - vorticalbox - Mar 4, 2019

(Mar 4, 2019 02:26 AM)lockhead Wrote:  I think he might want to keep it running in the background to automatically respond to incoming messages. Not sure tho.

@OP: Can you explain what the code is suppose to do and more importantly what exactly isn't working? If you don't want the program to keep running forever, get rid of the while loop entirely.

Doesn't reddit let you listen to events like Twitter api?

Doing a while true is going to lock the cpu.


RE: Why does this code doesn't work ? - nicodxmus - Mar 4, 2019

This in variables
Code
unread_messages = []
And this in ur while loop
Code
for item in reddit.inbox.unread(limit=None):
   if isinstance(item, Message):
       unread_messages.append(item)
reply = reddit.inbox(unread_messages)
reply.reply(message_to_reply)
reddit.inbox.mark_read(unread_messages)
I thought it should work