import linecache
for i in range (4):
file = open("looptestofreceivingquestions.txt", "r")
lineq = i+1
print(linecache.getline("looptestofreceivingquestions.txt", lineq))#gets line q depending on iteration
question = input("what is the answer?")
linea = i+5
answer = linecache.getline("looptestofreceivinganswers.txt", linea)
file.close()
print(question)
print(answer)
if question == answer:
print("correct")
elif question != answer:
print("wrong")不管怎么说,它都印错了。我正在做一个小测验,需要能够阅读文件中的问题和答案。for循环只重复每个问题和答案的代码。问题和答案也是一样的,这可以通过打印命令看到(例如,如果其中一个问题是2+2和我输出的4,它会说答案是4,答案是4)。对于问题和答案,我都使用了相同的文件,并且每个文件都存储在单独的行中。
发布于 2017-11-26 17:50:21
answer的末尾似乎有一个换行符(\n);我们必须去掉它:
answer = linecache.getline("looptestofreceivinganswers.txt", linea).rstrip('\n')https://stackoverflow.com/questions/47499083
复制相似问题