Machine Learning

To create an command questions app at R

The Saterinini Saceas Ninwinini Back, and I realized that most experiences have found that it often moves the data analysis and installation of the Oyori Codes.

Looking back, one of the benefits that I received from the scientific science increased the basic understanding of different planning languages.

Although reduced you have all of these ideas, but little so it doesn't work.

So in my mind, I challenged the building of something I used one of the best organizational tongues of data science: R.

And of course, I know what you might consider: Why r, not Python?

Well, you stick with me for a minute.

According to the Stratascratch Article, about 20,000 tested data, 31% are reported using daily.

Me, that 31% of a large pie piece, and thought.

If ur is strong enough to complete millions of data lines, why not use them and to make the edits of planning In relation to data science?

Sometimes, the best way to grow as a data scientist may not be jumping directly to the machine study or analyzing of large datasets. It may also come from reading regularly and gradually increase your skills.

That's what encouraged me to create this project, the Command-line Quiz app on R, there within the signal.

It is easy, but teaching the same skills you will need when creating complex data pipes, such as flowing, installation, installation, and molar operations.

In this article, I will go through the step by step by step, and share not only the code but also the lessons I have taken there.


Managing User Includes

I found little feelings here because this restored me when I used readline() In R. Seeing the program “listen” to be typing something that sounds like I'm talking to my code.

OK, many codes, less nostalgia.

Like many projects, I started a little, starting 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 gauge looks simple, but it introduces two important ideas:

  • readline() allows effective input to console.
  • tolower() + Celebrate trimws() It helps ordinary answers (to avoid mismatches due to the case or additional spaces).

When I started trying to try this, I used to “raise” for a consecutive place, and I noted badly. So, I realized that cleaning insulation is important as collecting.

LOGIC BUILDING CONTINUCTING CLOSE AND DOCUMENTS

Originally, I put everything inside one block of if Statements, but soon they were dide.

Not my greatest call, honesty.

It quickly reminded me of organized programs, where breaking things from work often creates clean code and easy 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 is heard to fill in the functions were not just a cleaning code, but notice that I would eventually work and sharpen my planning skills.

Data Science Type of Tiktok Dance; Really finds you when you start practicing it is moving.

Building a Bank of Questions

Reasing the questions, I needed a lot of way to keep many questions, instead of working hard at the same time at a time. I mean, you can do that, but it doesn't really work.

Now that is a Range of R List of R; It was flexible enough to catch questions and answers, which made it completely fit for what I was doing.

# 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 search list, I shared this with your friend who raised paragraphs (such as “geography” or “good planning” or “Formal Editing

Running questions (getting in question)

Now comes the fun part: To enter the question bank, to ask each question, and to keep school tracking. This loop is an engine that drives the entire system.

To do this, here is a simple flowchart of showing what I mean:

FlowChart (Photo by writer)

Through this form in mind, here is the appearance of the 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")

The last touch

In living organisms rise, it fously in a run_quiz() The work, making the system to re-activate 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!nn")
  
  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)

In the meantime, the app felt perfect. Accept the player, asked a series of questions, and showed last points for a celebration message.

Being clean.

The running of the sample

Here's what looked like when I played with 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

The end and the Knowledge

Looking back, this little project taught me the lessons that work directly from the traveling of the main science science tasks. Command-Line questions

When you are reading r, I recommend trying your version. Add further questions, and prevent them. In order to oppress yourself, you can even limit the time to answer.

The program is not in access to the finish line; It is about staying in the reading curve. Small projects such as you keep you move- one job, one loop, one challenge at a time.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button