Skip to content
← Back to Community
How to return a different message from a selection (Reddit PY)
Profile icon
KarmaIsADick

I'm making a Reddit bot. I want it to reply to a comment mentioning it's username with a random fact for a band I like, but it always returns the same one. See for yourself (here)[https://www.reddit.com/r/test/comments/p85e72/test_for_bot/].

And my code is here:
image

Answered by SixBeeps [earned 5 cycles]
View Answer
Voters
Profile icon
KarmaIsADick
Comments
hotnewtop
Profile icon
SixBeeps

You're only choosing a random number at the beginning of your code. From there on out, it doesn't pick another. There are a few ways of fixing this:

  • You could copy line 11 and lines 14-19 into the if statement between lines 24 and 25, which will pick a new fact with your current method.

  • A better solution would be to make a list of facts, then call the random class' choice() method on that list so that you don't have to make a bunch of if statements.

Profile icon
KarmaIsADick

Thanks! Your second solution did the trick. Here is 5 cycles for you. @SixBeeps

Profile icon
SixBeeps

@bennyrobert Neat, glad to hear I could help :)

Profile icon
KarmaIsADick

Hey, I'm having the same issue again. In the console it does, but on Reddit it is the same fact every time.
image
@SixBeeps

Profile icon
SixBeeps

@bennyrobert This one's easier to explain.

image

Here, you're saying fact is a list of all the facts contained in the facts file.

image

Then, when you get to sending a message, you're sending the whole list.

Instead of sending fact, you should instead send random.choice(fact).

Profile icon
KarmaIsADick

Thanks, but now I have this error:

image
I tried making it a global variable, but that didn't fix it.
@SixBeeps

Profile icon
KarmaIsADick

Oof, markdown fail. I didn't know how to a hyperlink!

Profile icon
SixBeeps

@bennyrobert Markdown syntax has brackets then parentheses. If you'd like you can edit the post to fix it ^^