如何从文本文件中随机选择问题

时间:2019-03-25 16:48:19

标签: python-3.x

我想从文本文件中随机抽取问题,以便创建大写测验。我需要重新格式化txt文件才能使“ random.shuffle”正常工作还是什么?

def askquestion(answer):  
    right = int(input("What is the right answer "))
    if right == answer:
        print("Correct")
    else:
        print("False")

questions = []
f = open("questiions.txt", "r")
lines = f.readlines()
f.close()

for line in lines:
    l = line.split(" ", 1)
    letter = l[0]
    text = l[1]
    if letter == "Q":
        answer = 0
        count = 1
    elif letter == "C":
         answer = count
    elif letter == "W":
          count = count + 1
    elif letter == "*":
        askquestion(answer)
    print(text)

这是TXT文件格式

Q What is the capital of Nauru
C Yaren
W Baiti
W Oro
* *
Q What is the capital of Niue
W Lome
C Alofi
W Lusaka
* *
Q What is the capital of Macedonia
W La Paz
C Skopje
W Rabat
* *

0 个答案:

没有答案