Home > Enterprise >  how to make variable pick randomly from a list in python
how to make variable pick randomly from a list in python

Time:04-09

So I need a variable to pick from a list I tried this code and it doesn't work. Any ideas?

list = ["R", "P","S"]
bot = random.choice(list)

CodePudding user response:

Did you import random?

You could manually build it out like so:

import random

def choice(a_list):
  return a_list[random.randrange(0,len(a_list)-1)]

print(choice([1,2,3]))

CodePudding user response:

use import random

import random
list = ["R", "P","S"]
bot = random.choice(list)
print(bot)
  • Related