6

How to make python choose randomly between multiple strings?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
user3814184
  • 97
  • 1
  • 1
  • 3
  • possible duplicate of [How do I randomly select an item from a list using Python?](http://stackoverflow.com/questions/306400/how-do-i-randomly-select-an-item-from-a-list-using-python) – Dan Hlavenka Jul 07 '14 at 23:57
  • 2
    Paste your code into your question, don't take a screenshot and post that on an external site... – Wooble Jul 08 '14 at 00:04
  • Heya, welcome to Stack Overflow! Generally it's Good Form to include your code here. It's ok to include a link to a fiddle like you have here - but we also want to see your code... don't forget that programmers are leery of clicking on links to external sites in case they are spam or phishing attempts... ;) Also - that fiddle will go away sometime, and we want Stack overflow to be eternal... and the only way that will happen is if you put your code here. – Taryn East Jul 08 '14 at 00:05
  • Real code so we an copy/paste - not a screenshot :) – Taryn East Jul 08 '14 at 00:06
  • And *especially* not a screenshot too small to read :) – Tom Zych Jul 08 '14 at 00:34

2 Answers2

20

Add them all to a list, import random, then call the choice method like so:

In [1]: import random
In [2]: hello = ['hi', 'hello', 'yo', 'bonjour', 'hola', 'salaam']
In [3]: random.choice(hello)
Out[3]: 'bonjour'
Dair
  • 15,910
  • 9
  • 62
  • 107
Robeezy
  • 2,424
  • 3
  • 22
  • 28
  • No problem, apparently other users hated my comment though. Have fun with python! It's a great language. – Robeezy Jul 08 '14 at 00:26
-2

random.choice() is your friend.

Undo
  • 25,519
  • 37
  • 106
  • 129
David Neiss
  • 8,161
  • 2
  • 20
  • 21