Ask coding questions

← Back to all posts
How to return a different message from a selection (Reddit PY)
bennyrobert (46)

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)[].

And my code is here:

Answered by SixBeeps (5629) [earned 5 cycles]
View Answer
SixBeeps (5629)

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.

bennyrobert (46)

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

SixBeeps (5629)

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

bennyrobert (46)

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


SixBeeps (5629)

@bennyrobert This one's easier to explain.

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

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).

bennyrobert (46)

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

SixBeeps (5629)

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