python-3.x - 在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. ")
解决方案
使用邮编:
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. ")
推荐阅读
- python - 这个程序中找到我们获得连续相等值的次数的错误是什么
- react-native - 在 react native 上面临样式问题并实现更好的响应式布局
- python - 异步函数意外地相互阻塞
- java - 一个表单中的多个提交按钮
- android - 如何在 Android Java 中序列化 Firestore 时间戳?
- javascript - 点击函数内部的事件在点击外部触发
- javascript - Node.js - process.exit 并将整个 console.log 消息附加到文本文件中
- controller - Metrics/AbcSize:创建的分配分支条件大小太高。[15.17/15]
- typescript - 用于在 VS Code 中以捷克语在 TypeScript 中显示错误消息的语言环境
- hadoop - 文件传输到 HDFS