我想从文本文件中随机抽取问题,以便创建大写测验。我需要重新格式化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
* *