I science journey a few years back, and I spotted that the majority of the experiences I gained tended to revolve around data evaluation and theoretical coding.
Looking back, one among the advantages I got from being a pc science major was developing a core understanding of varied programming languages.
Although the downside is that you have got all these theories, but little to no practice.
With that in mind, I challenged myself to construct something using one among the highest programming languages in data science: R.
And yes, I do know what you may be pondering:
Well, follow me for a minute.
In keeping with a StrataScratch article, nearly 20,000 data professionals were surveyed, and 31% reported using R each day.
To me, that 31% is a big slice of the pie, and it got me pondering.
If R is powerful enough to crunch thousands and thousands of rows of knowledge, why dont I also use it to practice the basics of programming in relation to data science?
Sometimes, one of the best solution to grow as an information scientist is probably not by jumping straight into machine learning libraries or analyzing large datasets. It could also come from embracing constant learning and step by step expanding your skills.
That’s what inspired me to create this project, a command-line quiz application in R, right contained in the terminal.
It’s easy, nevertheless it teaches the identical skills you’ll need when constructing more complex data pipelines, akin to control flow, input handling, and modular functions.
In this text, I’ll walk you thru the method step-by-step, sharing not only the code but in addition the teachings I picked up along the way in which.
Handling User Input
I got somewhat emotional here because this took me back to the primary time I used readline()
in R. Seeing this system “wait” for me to type something felt like I used to be having a conversation with my code.
Okay, more coding, less nostalgia.
Like most projects, I began small, starting with only one query and one answer check.
# First experiment: single query with basic input handling
# Bug note: without tolower(), "Abuja" vs "abuja" caused a mismatch
answer <- readline(prompt = "What's the capital of Nigeria? ")
if (tolower(trimws(answer)) == "abuja") {
cat("✅ Correct!n")
} else {
cat("❌ Incorrect. The right answer is Abuja.n")
}
This snippet looks easy, nevertheless it introduces two necessary ideas:
readline()
allows interactive input within the console.tolower()
+trimws()
helps normalize responses (avoiding mismatches because of case or extra spaces).
Once I first tried this, I typed “Abuja ” with a trailing space, and it marked me unsuitable. With that, I spotted that cleansing input is just as necessary as collecting it.
Constructing Logic with Control Flow and Functions
Initially, I stacked every thing inside a single block of if
statements, nevertheless it quickly became messy.
Not my best call, to be honest.
It quickly jogged my memory of structured programming, where breaking things into functions often makes the code cleaner and easier to read.
# Turned the input logic right 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("❌ Mistaken. The right answer is:", a, "n")
return(0)
}
}
# Quick test
ask_question("What's the capital of Nigeria?", "Abuja")
What felt most fulfilling about using functions wasn’t just the cleaner code, but the conclusion that I used to be finally practicing and sharpening my programming skills.
Data science is form of like learning a TikTok dance; you simply really get it once you begin practicing the moves yourself.
Making a Query Bank
To scale the quiz, I needed a solution to store multiple questions, as an alternative of just hardcoding one by one. I mean, you might try this, nevertheless it’s probably not efficient.
Now that’s the great thing about R’s list structure; it was flexible enough to carry each the questions and their answers, which made it an ideal fit for what I used to be constructing.
# Query bank: keeping it easy with an inventory of lists
# Note: began with just 2 questions before scaling up
quiz_questions <- list(
list(query = "What's the capital of Nigeria?", answer = "Abuja"),
list(query = "Which package is usually used for data visualization in R?", answer = "ggplot2")
)
# Later I added more, but this small set was enough to check the loop first.
In my quest to hunt feedback, I shared this with a friend who suggested adding categories (like “Geography” or “R Programming”), which could actually be a great improvement for later.
Running the Quiz (Looping Through Questions)
Now comes the fun part: looping through the query bank, asking each query, and keeping track of the rating. This loop is the engine that drives the complete application.
To make this clearer, here’s an easy flowchart as an example what I’m saying:
With this structure in mind, here’s the way it looks in code:
# Running through the quiz with a rating counter
# (I began with a for loop before wrapping this into run_quiz())
rating <- 0
for (q in quiz_questions) {
rating <- rating + ask_question(q$query, q$answer)
}
cat("📊 Your rating is:", rating, "out of", length(quiz_questions), "n")
Final Touches
To shine things up, I wrapped the logic right into a run_quiz()
function, making this system reusable and straightforward to grasp.
# Wrapped every thing in a single function for neatness
# This version prints a welcome message and total rating
run_quiz <- function(questions) {
rating <- 0
total <- length(questions)
cat("👋 Welcome to the R Quiz Game!n")
cat("You can be asked", total, "questions. Good luck!nn")
for (q in questions) {
rating <- rating + ask_question(q$query, q$answer)
}
cat("🎉 Final rating:", rating, "out of", total, "n")
}
# Uncomment to check
# run_quiz(quiz_questions)
At this point, the app felt complete. It welcomed the player, asked a series of questions, and displayed the ultimate rating with a celebratory message.
Neat.
Sample Run
Here’s what it looked like after I played it within the R console:
👋 Welcome to the R Quiz Game!
You can be asked 2 questions. Good luck!
What's the capital of Nigeria?
Your answer: Abuja
✅ Correct!
Which package is usually used for data visualization in R?
Your answer: ggplot
❌ Mistaken. The right answer is: ggplot2
🎉 Final rating: 1 out of two
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's a robust exercise.
For those who’re learning R, I like to recommend trying your personal version. Add more questions, and shuffle them. To push yourself more, you might even time-limit responses.
Programming isn’t about reaching a finish line; it’s about staying on the training curve. Small projects like this keep you moving forward— one function, one loop, one challenge at a time.