Попробуйте это, чтобы каждый раз использовать разные факты, пока список не будет исчерпан. Вы также получаете обновленный список каждый раз, когда ваш бот перезагружается.
all_fun_facts = ["Banging your head against a wall for one hour burns 150 calories.",
"Snakes can help predict earthquakes.",
"7% of American adults believe that chocolate milk comes from brown cows.",
"If you lift a kangaroo’s tail off the ground it can’t hop.",
"Bananas are curved because they grow towards the sun."]
fun_facts = all_fun_facts.copy()
@client.event
async def on_message(message):
if message.content.startswith("!funfact"):
try:
fact = random.choice(fun_facts)
except IndexError: # the list of fun facts is empty
fun_facts = all_fun_facts.copy()
fact = random.choice(fun_facts)
await message.channel.send(fact)
fun_facts.remove(fact)
Если вы хотите, чтобы список обновлялся только тогда, когда он пуст, попробуйте записать свой список в файл:
from fun_facts import all_fun_facts
fun_facts = all_fun_facts.copy()
@client.event
async def on_message(message):
if message.content.startswith("!funfact"):
try:
fact = random.choice(fun_facts)
except IndexError: # the list of fun facts is empty
fun_facts = all_fun_facts.copy()
fact = random.choice(fun_facts)
await message.channel.send(fact)
fun_facts.remove(fact)
Пример файловое хранилище (fun_facts.py
)
__all__ = ['all_fun_facts']
all_fun_facts = ["Banging your head against a wall for one hour burns 150 calories.",
"Snakes can help predict earthquakes.",
"7% of American adults believe that chocolate milk comes from brown cows.",
"If you lift a kangaroo’s tail off the ground it can’t hop.",
"Bananas are curved because they grow towards the sun."]