I science journey a couple of years back, and I realized that most of the experiences I gained tended to revolve around data analysis and theoretical coding.
Looking back, one of the benefits I got from being a computer science major was developing a core understanding of various programming languages.
Although the downside is that you have all these theories, but little to no practice.
With that in mind, I challenged myself to build something using one of the top programming languages in data science: R.
And yes, I know what you might be thinking: why R, and not Python?
Well, stick with me for a minute.
According to a StrataScratch article, nearly 20,000 data professionals were surveyed, and 31% reported usin…
I science journey a couple of years back, and I realized that most of the experiences I gained tended to revolve around data analysis and theoretical coding.
Looking back, one of the benefits I got from being a computer science major was developing a core understanding of various programming languages.
Although the downside is that you have all these theories, but little to no practice.
With that in mind, I challenged myself to build something using one of the top programming languages in data science: R.
And yes, I know what you might be thinking: why R, and not Python?
Well, stick with me for a minute.
According to a StrataScratch article, nearly 20,000 data professionals were surveyed, and 31% reported using R daily.
To me, that 31% is a huge slice of the pie, and it got me thinking.
If R is powerful enough to crunch millions of rows of data, why dont I also use it to practice the fundamentals of programming in relation to data science?
Sometimes, the best way to grow as a data scientist may not be by jumping straight into machine learning libraries or analyzing large datasets. It could also come from embracing constant learning and gradually expanding your skills.
That’s what inspired me to create this project, a command-line quiz application in R, right inside the terminal.
It’s simple, but it teaches the same skills you’ll need when building more complex data pipelines, such as control flow, input handling, and modular functions.
In this article, I’ll walk you through the process step by step, sharing not only the code but also the lessons I picked up along the way.
Handling User Input
I got a little emotional here because this took me back to the first time I used readline()
in R. Seeing the program “wait” for me to type something felt like I was having a conversation with my code.
Okay, more coding, less nostalgia.
Like most projects, I started small, beginning with just one question and one answer check.
# First experiment: single question with basic input handling
# Bug note: without tolower(), "Abuja" vs "abuja" caused a mismatch
answer <- readline(prompt = "What is the capital of Nigeria? ")
if (tolower(trimws(answer)) == "abuja") {
cat("✅ Correct!\n")
} else {
cat("❌ Incorrect. The correct answer is Abuja.\n")
}
This snippet looks simple, but it introduces two important ideas:
readline()
allows interactive input in the console.tolower()
+trimws()
helps normalize responses (avoiding mismatches due to case or extra spaces). When I first tried this, I typed “Abuja ” with a trailing space, and it marked me wrong. With that, I realized that cleaning input is just as important as collecting it.
Building Logic with Control Flow and Functions
Initially, I stacked everything inside a single block of if
statements, but it quickly became messy.
Not my greatest call, to be honest.
It quickly reminded me of structured programming, where breaking things into functions often makes the code cleaner and easier to read.
# Turned the input logic into a reusable function
# Small bug fix: added trimws() to remove stray spaces in answers
ask_question <- function(q, a) {
response <- readline(prompt = paste0(q, "\nYour answer: "))
if (tolower(trimws(response)) == tolower(a)) {
cat("✅ Correct!\n")
return(1)
} else {
cat("❌ Wrong. The correct answer is:", a, "\n")
return(0)
}
}
# Quick test
ask_question("What is the capital of Nigeria?", "Abuja")
What felt most fulfilling about using functions wasn’t just the cleaner code, but the realization that I was finally practicing and sharpening my programming skills.
Data science is kind of like learning a TikTok dance; you only really get it once you start practicing the moves yourself.
Creating a Question Bank
To scale the quiz, I needed a way to store multiple questions, instead of just hardcoding one at a time. I mean, you could do that, but it’s not really efficient.
Now that’s the beauty of R’s list structure; it was flexible enough to hold both the questions and their answers, which made it a perfect fit for what I was building.
# Question bank: keeping it simple with a list of lists
# Note: started with just 2 questions before scaling up
quiz_questions <- list(
list(question = "What is the capital of Nigeria?", answer = "Abuja"),
list(question = "Which package is commonly used for data visualization in R?", answer = "ggplot2")
)
# Later I added more, but this small set was enough to test the loop first.
In my quest to seek feedback, I shared this with a friend who suggested adding categories (like “Geography” or “R Programming”), which could actually be a good improvement for later.
Running the Quiz (Looping Through Questions)
Now comes the fun part: looping through the question bank, asking each question, and keeping track of the score. This loop is the engine that drives the entire application.
To make this clearer, here’s a simple flowchart to illustrate what I’m saying:
Flowchart (Image by Author)
With this structure in mind, here’s how it looks in code:
# Running through the quiz with a score counter
# (I started with a for loop before wrapping this into run_quiz())
score <- 0
for (q in quiz_questions) {
score <- score + ask_question(q$question, q$answer)
}
cat("📊 Your score is:", score, "out of", length(quiz_questions), "\n")
Final Touches
To polish things up, I wrapped the logic into a run_quiz()
function, making the program reusable and easy to understand.
# Wrapped everything in a single function for neatness
# This version prints a welcome message and total score
run_quiz <- function(questions) {
score <- 0
total <- length(questions)
cat("👋 Welcome to the R Quiz Game!\n")
cat("You will be asked", total, "questions. Good luck!\n\n")
for (q in questions) {
score <- score + ask_question(q$question, q$answer)
}
cat("🎉 Final score:", score, "out of", total, "\n")
}
# Uncomment to test
# run_quiz(quiz_questions)
At this point, the app felt complete. It welcomed the player, asked a series of questions, and displayed the final score with a celebratory message.
Neat.
Sample Run
Here’s what it looked like when I played it in the R console:
👋 Welcome to the R Quiz Game!
You will be asked 2 questions. Good luck!
What is the capital of Nigeria?
Your answer: Abuja
✅ Correct!
Which package is commonly used for data visualization in R?
Your answer: ggplot
❌ Wrong. The correct answer is: ggplot2
🎉 Final score: 1 out of 2
Conclusion and Takeaways
Looking back, this small project taught me lessons that directly apply to larger data science workflows. A command-line quiz game in R might sound trivial, but trust me, it is a powerful exercise.
If you’re learning R, I recommend trying your own version. Add more questions, and shuffle them. To push yourself more, you could even time-limit responses.
Programming isn’t about reaching a finish line; it’s about staying on the learning curve. Small projects like this keep you moving forward— one function, one loop, one challenge at a time.