尝试为 Python 中的测验中的正确答案加分

Trying to add up points for correct answers in a quiz in Python

我正在尝试获取一个程序来为正确答案加分。我应该在三个单独的列表中列出我的问题、正确答案和候选答案。一次要问一个问题,我必须 return 答案是否正确。然后我应该为每个正确答案加分。当我 运行 我的代码时,它只在正确答案后显示 1。我试过将 points 变量放在不同的地方,看看是否可行做。

我的功能如下。

def ask_questions():
#create lists for questions, correct answers, and candidate answers
    question_1 = '1) Who was the first American woman in space? '
    correct_answer_1 = 'Sally Ride'
    candidate_answer_1 = input(question_1)
    
    question_2 = '2) True or false: 5 kilometer == 5000 meters? '
    correct_answer_2 = 'true'
    candidate_answer_2 = input(question_2)
    
    question_3 = '3) (5 + 3)/2 * 10 = ? '
    correct_answer_3 = '40'
    candidate_answer_3 = input(question_3)
    
    question_4 = "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? "
    correct_answer_4 = 'Trajectory'
    candidate_answer_4 = input(question_4)
    
    question_5 = '5) What is the minimum crew size for the ISS? '
    correct_answer_5 = '3'
    candidate_answer_5 = input(question_5)
    
    #lists that correlate to the variables assigned above
    
    quiz_questions = [question_1, question_2, question_3, question_4, question_5]
    correct_answers = [correct_answer_1, correct_answer_2, correct_answer_3, correct_answer_4, correct_answer_5]
    candidate_answers = [candidate_answer_1, candidate_answer_2, candidate_answer_3, candidate_answer_4, candidate_answer_5]
    
    index = 0
    # points = 0
    # total_score = (points/5) * 100
    for item in quiz_questions:
        points = 0
        if candidate_answers[index].lower() == correct_answers[index].lower():
            points += 1
            print(f'Your Answer: {candidate_answers[index]}\nCorrect Answer: {correct_answers[index]}') 
        elif candidate_answers[index] != correct_answers[index]:
            print('Incorrect.\nThe correct answer is: ', correct_answers[index])
        index += 1
        print(points)
        
    
ask_questions()

主要问题是您在循环内将 points 重置为 0,这意味着它只能是 01index 的业务令人困惑,并且可能难以调试 points 的东西;我建议只使用 zip 来使整个事情变得更容易:

points = 0
for correct, candidate in zip(correct_answers, candidate_answers):
    if correct.lower() == candidate.lower():
        points += 1
        print(f'Your Answer: {candidate}\nCorrect Answer: {correct}') 
    else:
        print('Incorrect.\nThe correct answer is: ', correct)
print(points)

Samwise 是正确的,在 for 循环的每次迭代中重新分配 points 存在问题。但是,您可以通过将所有 question-answer 对存储在元组列表中,然后一次对它们进行迭代以收集用户的答案,从而使您的代码更简洁:

def ask_questions():
    quiz_questions = [
        ('1) Who was the first American woman in space? ', 'Sally Ride'),
        ('2) True or false: 5 kilometer == 5000 meters? ', 'true'),
        ('3) (5 + 3)/2 * 10 = ? ', '40'),
        ("4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ", 'Trajectory'),
        ('5) What is the minimum crew size for the ISS? ', '3')
    ]
    submitted_answers = []
    
    for question, _ in quiz_questions:
        submitted_answers.append(input(question))
        
    points = 0
    for answer, (_, correct_answer) in zip(submitted_answers, quiz_questions):
        if answer.lower() == correct_answer.lower():
            points += 1
            print(f'Your Answer: {answer}\nCorrect Answer: {correct_answer}') 
        else:
            print('Incorrect.\nThe correct answer is: ', correct_answer)
    print(points)

您在循环内声明了点,问题是每次迭代都会将点重置回 0 并修复只是将其移出 for 循环。

来自:

for item in quiz_questions:
    points = 0
    ...

至:

points = 0
for item in quiz_questions:
    ...

不过我确实认为您的代码可以写得更简洁一些:

questions = [
    {
        "question": '1) Who was the first American woman in space? ',
        "answer": 'Sally Ride'
    },
    {
        "question": '2) True or false: 5 kilometer == 5000 meters? ',
        "answer": 'true'
    },
    {
        "question": '3) (5 + 3)/2 * 10 = ? ',
        "answer": '40'
    },
    {
        "question": "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ",
        "answer": 'Trajectory'
    },
    {
        "question": '5) What is the minimum crew size for the ISS? ',
        "answer": '3'
    },
]

points = 0
for i in questions:
    if input(i['question']).lower() == i['answer'].lower():
        print('correct')
    else:
        print(f"Incorrect.\nThe correct answer is: {i['answer']}")
    print(points)