toBePythonNinja
toBePythonNinja

Reputation: 57

Unsure how to select one random output from this list in python

I would like to select one random update.message from that list when the music funtion is called. I assume I should store all the links outside of the update.message so it doesnt send all 3 at the call of music

def music(bot, update):

    update.message.reply_text("https://www.youtube.com/watch?v=XErrOJGzKv8")
    update.message.reply_text("https://www.youtube.com/watch?v=hHW1oY26kxQ")
    update.message.reply_text("https://www.youtube.com/watch?v=RmHg6BP5D8g")

Upvotes: 2

Views: 201

Answers (4)

Nuwan
Nuwan

Reputation: 505

use this

import random
foo=['link1','link2','link3']
random_link=random.choice(foo)
#call your function here using random_link

refer this question too How to randomly select an item from a list?

Upvotes: 2

Felipe
Felipe

Reputation: 130

First step: store your values in a list.

Second step: just use random.choice()

import random

my_links = [
    "https://www.youtube.com/watch?v=XErrOJGzKv8",
    "https://www.youtube.com/watch?v=hHW1oY26kxQ",
    "https://www.youtube.com/watch?v=RmHg6BP5D8g"
]


def music(bot, update):
    update.message.reply_text(random.choice(my_links))

Upvotes: 0

Wong Siwei
Wong Siwei

Reputation: 113

Use random.choice(seq)

import random

def music (bot, update):

    list_of_urls = ["www.your-urls.com", "www.your-urls2.com"]
    random_url = random.choice(list_of_urls)

    update.message.reply_text(random_url)

Upvotes: 0

boop
boop

Reputation: 36

random is great at choosing something random.

import random
playlist = ['https://www.youtube.com/watch?v=XErrOJGzKv8','https://www.youtube.com/watch?v=hHW1oY26kxQ', 'https://www.youtube.com/watch?v=RmHg6BP5D8g']

def music(bot, update):
    update.message.reply_text(random.choice(playlist)) # random.choice() chooses a random item from a list

Upvotes: 0

Related Questions