首页 > 解决方案 > 在python列表中的索引之间移动

问题描述

在 Python 中,我将文件中的信息填充到列表中,然后打印列表中的第一个索引,现在我想打印列表中的下一个索引而不需要多个我的代码。

如何推广我正在打印的索引?

question = open("questions.txt", "r")

print(question.readlines()[0])

tanswer = open("answers.txt", "r")

correct_answer = float(tanswer.readlines()[0])

uanswer = float(input("Write the answer: "))

if correct_answer==uanswer:
    print("Amazing " + str(correct_answer) + " is the correct answer")
else:
    print("Wrong " + str(uanswer) + " is not correct, Try again please. ")

标签: python-3.x

解决方案


使用邮编:

with open("questions.txt") as ques, open("answers.txt") as ans:
    for q, a in zip(ques, ans):
        print(q)
        uanswer = input("Write the answer: ")

        if float(a) == float(uanswer):
            print("Amazing " + str(a) + " is the correct answer")
        else:
            print("Wrong " + str(uanswer) + " is not correct, Try again please. ")

推荐阅读