You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python填空题测验开发遇阻:输入答案后提示错误,求技术建议

Troubleshooting Your Python Fill-in-the-Blank Quiz Answer Matching Issue

Hey there! Let's dig into why your quiz keeps showing "not quite, please try again" even when you enter the right answer. This is a super common hiccup with input-based quizzes, and it almost always comes down to small mismatches between how you handle user input and how you store/compare the correct answer. Here are the top things to check:

1. Case Sensitivity & Extra Whitespace

This is the #1 culprit. If your correct answer is Def but the user types def, or if the user accidentally adds a space at the end (like def ), the direct comparison will fail. Fix this by normalizing both the user input and the correct answer:

  • Use .strip() to remove leading/trailing spaces, tabs, or newlines
  • Use .lower() (or .upper()) to make the comparison case-insensitive

Example fix in code:

# Instead of this:
if user_input == correct_answer:

# Do this:
if user_input.strip().lower() == correct_answer.strip().lower():

2. Mismatched Answer Storage/Retrieval

Double-check that you're pulling the right correct answer for the current question. For example:

  • If you're using a list of questions and answers, make sure the index for the answer matches the current question index
  • If you're using a dictionary, confirm you're accessing the correct key (like question["answer"] instead of a typo like question["correct"])

Here's a clean, reliable quiz structure that avoids this issue:

quiz = [
    {
        "prompt": "What built-in function returns the length of an object?",
        "answer": "len"
    },
    {
        "prompt": "What keyword is used to exit a loop early?",
        "answer": "break"
    }
]

for question in quiz:
    user_response = input(f"{question['prompt']} ").strip().lower()
    if user_response == question['answer'].lower():
        print("Perfect, that's right!")
    else:
        print("not quite, please try again")

3. Type Mismatches

If your answer is a number (like 42) but the user input is a string (since input() always returns a string), comparing them directly will fail. For example:

# This will fail if user enters "42"
correct_answer = 42
user_input = input("What's 6*7? ")
if user_input == correct_answer:  # String vs integer comparison
    print("Correct!")

Fix this by converting the user input to the right type (or the answer to a string):

# Convert input to integer
if int(user_input.strip()) == correct_answer:
# OR convert answer to string
if user_input.strip() == str(correct_answer):

Quick Debugging Trick

Add a print statement right before your comparison to see exactly what you're comparing. This will reveal hidden issues you might miss:

print(f"User input: '{user_input}' | Correct answer: '{correct_answer}'")

Give these checks a go—chances are one of them is the fix you need!

内容的提问来源于stack exchange,提问作者Gary Moore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:48:02