想了解如何运行,最好的办法就是分步执行看看运行结果。
首选,这个代码中貌似有错误吧,第9行中的变量名应该是wordIndex吧。
接着,说说其中的关键代码吧。
wordList=list(random.choice(wordsList))
//以上代码是从四个单词中随机选择一个单词
wordIndertex=random.sample(list(range(0,wordListlen)),len(wordList))
//list(range(0,wordListlen),生成一串数字,从0开始,最大值为单词中的字母数减1
//然后从这串数字中随机选择几个数字,这里的个数是len(wordList),即全部选择,所以这行代码的作用相当于打乱顺序
tipsList=['-']*wordListlen
//生成一个列表,元素都是'-',元素个数为wordListlen
id0=wordIndex[0]
id1=wordIndex[1]
//以上两行代码表示获取到随机选择的数字的第一个、第二个
tipsList[id0]=wordList[id0]
tipsList[id1]=wordList[id1]
//最后,获取到这个单词中的索引为id0和id1的字母
自己运行一下,看看每一步的运行结果就明白了。