Menu
Coddy logo textTech

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
  • 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

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

local gardenRoom = {
    description = "You are in a beautiful garden with blooming flowers and a fountain."
}

-- Player definition
local player = {
    currentRoom = startingRoom
}

-- Create game loop
local gameRunning = true

while gameRunning do
    print(player.currentRoom.description)
    gameRunning = false
end

All lessons in Logic & Flow