Menu
CoddyTech

Handling User Input

Part of the Logic & Flow section of Coddy's Lua journey. Lesson 28 of 54.

challenge icon

Challenge

Easy

Extend the game loop to read and display user commands using io.read().

You are provided with the startingRoom, gardenRoom, and player variables from the previous challenge.

Modify the game loop to:

  • Print the description of the player's current room
  • Print the prompt "> " (greater-than sign followed by a space) without a newline: use io.write() instead of print(), since io.write() outputs text without adding a newline at the end
  • Read the user's command using io.read()
  • Print "You entered: " followed by the command the user typed
  • Check if the command is "quit" - if so, set gameRunning to false to exit the loop

The loop should continue until the user types "quit".

Expected Output Format:

You find yourself in a dusty library filled with ancient books and scrolls.
> You entered: look
You find yourself in a dusty library filled with ancient books and scrolls.
> You entered: east
You find yourself in a dusty library filled with ancient books and scrolls.
> You entered: quit

Try it yourself

-- Game rooms
local startingRoom = {
    description = "You find yourself in a dusty library filled with ancient books and scrolls."
}

local gardenRoom = {
    description = "You step into a beautiful garden with blooming flowers and a gentle breeze."
}

-- Player
local player = {
    currentRoom = startingRoom
}

-- Game loop
local gameRunning = true

-- TODO: Write the while loop here

All lessons in Logic & Flow

Practice on your own: Online Lua compiler