CodeBlanks Logo Icon CodeBlanks

Score calculation

Learn correct count with the score calculation method.

Score tracking

To evaluate the quiz we write a grade method. It compares a given answer list with the question answers and writes the correct count to the score field. Then it prints the score.

The Quiz class gets an int score = 0; field. The grade(List<String> answers) method compares each answer with questions[i].answer; if equal it runs score++.

This structure is the basis of automatic answer evaluation. Real apps read answers from the user; here we simulate with a fixed list.

Showing the result

After the loop the grade method prints the result with print('Score: $score'). If one of two answers is correct the output is Score: 1.

$score is string interpolation; it embeds the score variable value into the text. In Dart $variable gives the variable value.

When this method is called the quiz is evaluated. The next lesson combines all pieces into a working app.

Practice Add the grade method and score field

Add an int score = 0 field to the Quiz class. Then write void grade(List<String> answers): start with score = 0, loop with for to compare each answer with questions[i].answer, run score++ on a match. After the loop write print('Score: $score'). The main already calls quiz.grade(['Ankara', 'Wrong']).

Starter code

main.dart
class Question {
  String text;
  String answer;
  Question(this.text, this.answer);
}

class Quiz {
  List<Question> questions = [];
  // add the score field
  void addQuestion(Question q) {
    questions.add(q);
  }
  void play() {
    for (var i = 0; i < questions.length; i++) {
      print('${i + 1}. ${questions[i].text}');
    }
  }
  // add the grade method
}

void main() {
  var quiz = Quiz();
  quiz.addQuestion(Question('What is the capital?', 'Ankara'));
  quiz.addQuestion(Question('How many days in a week?', '7'));
  quiz.grade(['Ankara', 'Wrong']);
}

Sample solution

main.dart
class Question {
  String text;
  String answer;
  Question(this.text, this.answer);
}

class Quiz {
  List<Question> questions = [];
  int score = 0;
  void addQuestion(Question q) {
    questions.add(q);
  }
  void play() {
    for (var i = 0; i < questions.length; i++) {
      print('${i + 1}. ${questions[i].text}');
    }
  }
  void grade(List<String> answers) {
    score = 0;
    for (var i = 0; i < questions.length; i++) {
      if (answers[i] == questions[i].answer) {
        score++;
      }
    }
    print('Score: $score');
  }
}

void main() {
  var quiz = Quiz();
  quiz.addQuestion(Question('What is the capital?', 'Ankara'));
  quiz.addQuestion(Question('How many days in a week?', '7'));
  quiz.grade(['Ankara', 'Wrong']);
}

Expected output

Score: 1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the score parts

Term Meaning
int score = 0 Score field
grade(List<String> answers) Grading method
answers[i] == questions[i].answer Comparison
score++ Correct count increment

2 When is answers[i] == questions[i].answer true?

main.dart
if (answers[i] == questions[i].answer) {
  score++;
}
  • When the answer is empty
  • When the answer is wrong
  • When the given answer matches the correct one Correct answer
  • As many times as questions

Explanation: the condition holds when the given answer equals the correct one.

3 Score increment

Question
void grade(List<String> answers) {
  score = 0;
  for (var i = 0; i < questions.length; i++) {
    if (answers[i] == questions[i].answer) {
      ____;
    }
  }
  print('Score: $score');
}
Answer
void grade(List<String> answers) {
  score = 0;
  for (var i = 0; i < questions.length; i++) {
    if (answers[i] == questions[i].answer) {
      score++;
    }
  }
  print('Score: $score');
}

Explanation: score++ raises the correct count by one.

All lessons in Intermediate