Py-Random function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Py-Random function

How do I get Py to say random word? What I mean is this -if I want PY to produce a random number between 1 to 10, I do import random x=random.randint(1,10) what do I do if I want PY to produce a random word, not number?

28th Apr 2018, 4:59 AM
Jinsu
2 Answers
+ 2
You make an array and do it so you print the word when the number is chosen: random.....(1,10) Array = [word,dkjejke, skeih] print(array[random])
28th Apr 2018, 5:09 AM
Ole113
Ole113 - avatar
+ 2
You have to store the words before using. In example in a list or a tuple. You could use the modul random and the method random.choice (source in braces) If you import random as r you have a little bit less to type. random.choice() = r.choice() --------------------------- import random as r ex_list = [ "hello", "world", "foo", "bar"] print(r.choice (ex_list))
28th Apr 2018, 2:48 PM
Sven_m
Sven_m - avatar