random module三个常用function: randint, choice, shuffle
import random print(random.randint(1,10)) #从1到10(两头都包含)中随机选择一个数 words = ['grable', 'craven', 'maven', 'mossback', 'faze', 'cagy', 'haggard', 'fogy'] random_word = random.choice(words) #从words list中随机选择一个 print(random_word) random.shuffle(words) #将words list随机重新排序 print(words)
一种可能的输出结果为:
3 mossback ['mossback', 'fogy', 'cagy', 'faze', 'craven', 'haggard', 'maven', 'grable']