我试图在Python中创建一个测验程序,其中问题存储在一个txt文件中,答案存储在另一个文件中。问题在文本文件中列出如下:
Which one of these is a percussion instrument?
A. Trumpet
B. Euphonium
C. Viola
D. Glockenspiel
该程序以随机顺序提取问题并保持正确答案数量的得分。我知道如何打开文件,从中读取文件并在屏幕上显示文件的内容,我现在甚至知道如何随机化文件中的信息。但是,由于涉及多行和另一个文件来获得答案,我不知道从哪里开始。我真的很感激你能提供给我的任何帮助。如果您需要澄清任何问题,请随时提出问题。
编辑:好的,我已经决定改变我的想法,这可能会使它更容易。使用CSV文件可能是更好的选择。这是我到目前为止所拥有的。
def Trivia():
score=0
myFile = open("farming.csv","r") # opens the CSV file and stores it in the array myFile
players = myFile.readlines() # reads the lines of the CSV file into the variable players
questionno=1
while questionno < 6:
for p in players:
data = p.split(",") #splits each cell of the CSV file into its parts
questions = data[0]
answera = data[1]
answerb = data[2]
answerc = data[3]
CorrectAnswer = data[4]
print("Question #",questionno)
print(questions) #prints the question and the 3 answers
time.sleep(0.5)
print(answera)
time.sleep(0.5)
print(answerb)
time.sleep(0.5)
print(answerc)
time.sleep(0.5)
answer = input("Answer? ") #asks the user for their answer
time.sleep(1)
print(".")
time.sleep(1)
print(".")
time.sleep(1)
print(".")
if answer == CorrectAnswer: #checks if the answer is correct and prints approptiate responses
print("That is the correct answer")
score=score+1
time.sleep(1)
else:
print("That is not the correct answer")
time.sleep(1)
print("Your current score is", score)
print("")
questionno = questionno+1
myFile.close()
我现在的问题是我不知道如何进入测验中的下一个问题。使用这种格式,它一直在问同样的问题。任何的想法?
谢谢。
我不是百分之百确定,但我还没有自己运行该程序来检查。但我认为它可能是“While”模块。它说虽然问号是六岁以下,做那个问题,所以当你向问题添加1时,它仍然在6以下再次运行该程序。也改变它
如果questionno == 1:..... ..... .....
如果questionno == 2:..... ..... .....