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

如何在Python中创建计分系统?求答题计分示例及代码指导

Python答题计分系统实现指南与示例

Hey there! Let's sort out your quiz scoring system and put together a complete, working example where correct answers add 1 point and the total score is shown at the end.

First, let's fix the issues in your current code:

  • The score variable isn't initialized before you try to update it — this will throw an UnboundLocalError when you run the code. You need to start with a score of 0.
  • Checking for 'Bonjour' and 'bonjour' separately is redundant; you can use a case-insensitive check to simplify this.

Here's a polished, complete version of your quiz system:

def run_quiz():
    # Initialize score to 0 at the start of the quiz
    score = 0
    print("Challenge level 1 has been selected.")
    print("Can you translate these words into French?")

    # Question 1
    answer = input('Q1. Hello! : ')
    if answer.strip().lower() == 'bonjour':
        score += 1
        print('Correct!')
    else:
        print(f'Wrong! The correct answer is Bonjour')

    # Add more questions here
    # Question 2
    answer = input('Q2. Thank you! : ')
    if answer.strip().lower() == 'merci':
        score += 1
        print('Correct!')
    else:
        print(f'Wrong! The correct answer is Merci')

    # After all questions, display the total score
    print(f"\nQuiz complete! Your total score is: {score}/2")

# Run the quiz
run_quiz()

Key improvements explained:

  • Score initialization: We start with score = 0 so we have a valid variable to update as answers are marked correct.
  • Case-insensitive checking: answer.strip().lower() removes extra spaces and converts the input to lowercase, so inputs like " Bonjour " or "BONJOUR" will still count as correct.
  • Scalable structure: Adding more questions is straightforward — just repeat the question/answer check pattern.
  • Clear final score: At the end, we print out the total score with the maximum possible points for context.

Bonus: Make your quiz easier to maintain

If you plan to add lots of questions, storing them in a list of tuples will make your code cleaner:

def run_scalable_quiz():
    score = 0
    questions = [
        ("Hello!", "bonjour"),
        ("Thank you!", "merci"),
        ("Goodbye!", "au revoir"),
        ("Please!", "s'il vous plaît")
    ]

    print("Challenge level 1 has been selected.")
    print("Can you translate these words into French?")

    for idx, (question, correct_answer) in enumerate(questions, 1):
        answer = input(f'Q{idx}. {question} : ')
        if answer.strip().lower() == correct_answer:
            score += 1
            print('Correct!')
        else:
            # Capitalize the first letter for the feedback
            print(f'Wrong! The correct answer is {correct_answer.capitalize()}')

    print(f"\nQuiz complete! Your total score is: {score}/{len(questions)}")

run_scalable_quiz()

This way, you can add new questions to the questions list without writing extra if/else blocks — much cleaner for longer quizzes!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:00:58