python随机抽取单词记

怎么从文本中随机抽取单词来背呢

    def Backword(self):  # 背单词
        with open("danciben.txt", 'r') as worldFile:
            world_list = worldFile.readlines()
            for index in world_list:
                w = np.random.choice(index.split("\n"))
                c = index.split(".")[1]
                count = 1
                flag = True
                while count <= 3 and flag == True:
                    en = input(str(w) + "的汉语意思:")
                    if en + "\n" == c:
                        print("恭喜答对")
                        flag = False
                    if en + "\n" != c:
                        print("答错了,还有" + str(3 - count) + "次机会:")
                        count = count + 1
                flag = True
            print("您已经完成了单词本里的全部单词")

单词本大概如下

wind n.胸口,心窝
convey vt.传达;传播;转让
correctly ad.正确地,恰当地
clap n.拍手喝采声;霹雳声
coin vt.铸造(硬币)
popularity n.通俗性;普及,流行
abbreviation n.节略,缩写,缩短
abide vt.遵守;vt.忍受
abolish vt.废除,取消
absent a.不在意的
absorption n.吸收;专注
abstract a.理论上的;n.抽象
absurd a.不合理的,荒唐的
abundance n.丰富,充裕
accessory n.同谋;a.附属的
accord n.调和,符合;协议
acknowledge vt.承认;告知收到
acquaint vt.使认识,使了解
action n.作用;情节
adhere vi.粘附;追随;坚持
adjacent a.毗连的;紧接着的

下面是修改过的代码,你可以将这些修改添加到你的函数中:

import random

def Backword(self):  # 背单词
    with open("danciben.txt", 'r') as worldFile:
        world_list = worldFile.readlines()
        # 使用random.shuffle函数将单词列表打乱
        random.shuffle(world_list)
        for index in world_list:
            # 使用random.choice函数从单词列表中随机选择一个单词
            w = random.choice(index.split("\n"))
            c = index.split(".")[1]
            count = 1
            flag = True
            while count <= 3 and flag == True:
                en = input(str(w) + "的汉语意思:")
                if en + "\n" == c:
                    print("恭喜答对")
                    flag = False
                if en + "\n" != c:
                    print("答错了,还有" + str(3 - count) + "次机会:")
                    count = count + 1
            flag = True
        print("您已经完成了单词本里的全部单词")

这个修改后的函数会打乱单词列表的顺序,并从中随机选择单词进行背诵。