向用户询问列表索引,Python
Ask user for index of list, Python
这些词典快要死了!
这是我的JSON(问题),其中包含 2 个字典的列表。
[
{
"wrong3": "Nope, also wrong",
"question": "Example Question 1",
"wrong1": "Incorrect answer",
"wrong2": "Another wrong one",
"answer": "Correct answer"
},
{
"wrong3": "0",
"question": "How many good Matrix movies are there?",
"wrong1": "2",
"wrong2": "3",
"answer": "1"
}
]
我试图让用户搜索一个索引,然后列出该索引的值(如果有的话)。目前我提示用户输入然后找到问题的索引然后检查输入是否等于索引,现在它 returns False,即使输入了正确的索引也是错误的。我在正确的轨道上但在语义上是错误的吗?或者我应该研究另一条路线?
import json
f = open('question.txt', 'r')
questions = json.load(f)
f.close()
value = inputSomething('Enter Index number: ')
for i in questions:
if value == i:
print("True")
else:
print("False")
您正在循环访问列表值。 for i in questions
将遍历列表值而不是列表索引,
您需要遍历列表的索引。为此,您可以使用枚举。你应该试试这个方法..
for index, question_dict in enumerate(questions):
if index == int(value):
print("True")
else:
print("False")
在Python最好not check beforehand, but try and deal with error if it occurs.
这是遵循这种做法的解决方案。花点时间仔细看看这里的逻辑,它可能对你以后有帮助:
import json
import sys
# BTW: this is the 'pythonic' way of reading from file:
# with open('question.txt') as f:
# questions = json.load(f)
questions = [
{
"question": "Example Question 1? ",
"answer": "Correct answer",
},
{
"question": "How many good Matrix movies are there? ",
"answer": "1",
}
]
try:
value = int(input('Enter Index number: ')) # can raise ValueError
question_item = questions[value] # can raise IndexError
except (ValueError, IndexError) as err:
print('This question does not exist!')
sys.exit(err)
# print(question_item)
answer = input(question_item['question'])
# let's strip whitespace from ends of string and change to lowercase:
answer = answer.strip().lower()
if answer == question_item['answer'].strip().lower():
print('Good job!')
else:
print('Wrong answer. Better luck next time!')
这些词典快要死了!
这是我的JSON(问题),其中包含 2 个字典的列表。
[
{
"wrong3": "Nope, also wrong",
"question": "Example Question 1",
"wrong1": "Incorrect answer",
"wrong2": "Another wrong one",
"answer": "Correct answer"
},
{
"wrong3": "0",
"question": "How many good Matrix movies are there?",
"wrong1": "2",
"wrong2": "3",
"answer": "1"
}
]
我试图让用户搜索一个索引,然后列出该索引的值(如果有的话)。目前我提示用户输入然后找到问题的索引然后检查输入是否等于索引,现在它 returns False,即使输入了正确的索引也是错误的。我在正确的轨道上但在语义上是错误的吗?或者我应该研究另一条路线?
import json
f = open('question.txt', 'r')
questions = json.load(f)
f.close()
value = inputSomething('Enter Index number: ')
for i in questions:
if value == i:
print("True")
else:
print("False")
您正在循环访问列表值。 for i in questions
将遍历列表值而不是列表索引,
您需要遍历列表的索引。为此,您可以使用枚举。你应该试试这个方法..
for index, question_dict in enumerate(questions):
if index == int(value):
print("True")
else:
print("False")
在Python最好not check beforehand, but try and deal with error if it occurs.
这是遵循这种做法的解决方案。花点时间仔细看看这里的逻辑,它可能对你以后有帮助:
import json
import sys
# BTW: this is the 'pythonic' way of reading from file:
# with open('question.txt') as f:
# questions = json.load(f)
questions = [
{
"question": "Example Question 1? ",
"answer": "Correct answer",
},
{
"question": "How many good Matrix movies are there? ",
"answer": "1",
}
]
try:
value = int(input('Enter Index number: ')) # can raise ValueError
question_item = questions[value] # can raise IndexError
except (ValueError, IndexError) as err:
print('This question does not exist!')
sys.exit(err)
# print(question_item)
answer = input(question_item['question'])
# let's strip whitespace from ends of string and change to lowercase:
answer = answer.strip().lower()
if answer == question_item['answer'].strip().lower():
print('Good job!')
else:
print('Wrong answer. Better luck next time!')