+ 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?
2 ответов
+ 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])
+ 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))