Quiz Scoring
Score a quiz and provide a grade.
Example 087 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell QuizApp has no face and no memory. Handlers: demo (line 16).
The source
087_quiz_app.cell
// ============================================================================
// 087 — Quiz Scoring
// ============================================================================
// Score a quiz and provide a grade.
//
// Run: soma run 087_quiz_app.cell demo
// Expect:
// Q1: What is 2+2? Answer: 4 -> Correct!
// Q2: Capital of France? Answer: Paris -> Correct!
// Q3: Largest planet? Answer: Mars -> Wrong! (Jupiter)
// Score: 2/3 (66%)
// Grade: D
cell QuizApp {
on demo() {
let questions = list(
map("q", "What is 2+2?", "answer", "4", "response", "4"),
map("q", "Capital of France?", "answer", "Paris", "response", "Paris"),
map("q", "Largest planet?", "answer", "Jupiter", "response", "Mars")
)
let correct = 0
let total = questions.length
let num = 1
for item in questions {
let num_str = to_string(num)
if item.response == item.answer {
print("Q{num_str}: {item.q} Answer: {item.response} -> Correct!")
correct = correct + 1
} else {
print("Q{num_str}: {item.q} Answer: {item.response} -> Wrong! ({item.answer})")
}
num = num + 1
}
let pct = (correct * 100) / total
print("Score: {correct}/{total} ({pct}%)")
// Grade
let grade = "F"
if pct >= 90 {
grade = "A"
} else {
if pct >= 80 {
grade = "B"
} else {
if pct >= 70 {
grade = "C"
} else {
if pct >= 60 {
grade = "D"
}
}
}
}
print("Grade: {grade}")
return "done"
}
}What the toolchain says
$ soma check 087_quiz_app.cell
✓ All checks passed.$ soma run 087_quiz_app.cell demo
Q1: What is 2+2? Answer: 4 -> Correct!
Q2: Capital of France? Answer: Paris -> Correct!
Q3: Largest planet? Answer: Mars -> Wrong! (Jupiter)
Score: 2/3 (66.66666666666667%)
Grade: D
done