Part I · Getting It Running

Chapter 2: Talking Back and Forth

Right now your game only talks in one direction. It tells the player about the cottage, and that's the end of the conversation. This chapter fixes that. By the end of it, your program will ask the player something, listen to the answer, and use it.

Storing Things with var

A variable is just a named place to keep a value so you can use it again later. In QF Code, you create one with var.

QF Code example 1
var playerName = "Alice"
var health = 100
var isAlive = true

You don't tell QF Code what type of value a variable holds. You just give it a name and a starting value, and the language figures the rest out. That's what "dynamically typed" means, and it's one of the ways QF Code stays out of your way early on.

The Three Kinds of Values You'll Use Most

For now, three kinds of values will cover almost everything you write:

QF Code example 2
var score = 0          // a number
var name = "Alice"     // a string, in double quotes
var isReady = true     // a boolean, true or false

A quick note on that last line: QF Code doesn't care whether you write true, TRUE, or True. Keywords are case-insensitive. So are variable names, score and SCORE refer to the same variable. This book will stick to lowercase everywhere, and you're free to do the same.

Getting Input from the Player

To ask the player something, use input():

QF Code example 3
var playerName = input("What is your name? ")
writeln("Welcome, " + playerName + ".")

Run that, and the output panel gives you a text box to type into. Whatever you type becomes the value of playerName.

One thing worth knowing now, before it surprises you later: input() always returns a string, even if the player types a number. If you ask for someone's age, input() hands you back the text "12", not the number 12. You'll see why that matters in a moment.

Putting Strings Together

That + in the welcome message above is concatenation, joining two strings into one.

QF Code example 4
var greeting = "Welcome, " + playerName + "."

You can also mix a string with a number, boolean, or empty value, and QF Code converts the non-string side for you automatically.

QF Code example 5
writeln("Score: " + 10)      // Score: 10
writeln("Ready: " + true)    // Ready: TRUE

There's one case where QF Code refuses to guess, on purpose. If you try to add a number-looking string directly to an actual number, it stops and makes you say what you mean.

QF Code example 6
"3" + 4 // error: use str() or num() explicitly

That's not a bug, and it isn't QF Code being difficult. "3" + 4, is that supposed to become the number 7, or the string "34"? Two reasonable answers exist, and rather than silently pick one, QF Code makes you choose. Which brings us to conversion.

Converting Between Types

Three functions handle moving values between types on purpose:

QF Code example 7
str(4)          // "4", turns a value into its text form
num("4")        // 4, turns text into a number
bool("true")    // TRUE, turns certain text/number forms into a boolean

This is exactly what fixes the ambiguous case from a moment ago:

QF Code example 8
"3" + num("4")   // 7
"3" + str(4)     // "34"

And it's what makes input() usable for anything numeric. Since input() always hands back a string, asking for an age looks like this:

QF Code example 9
var age = num(input("How old are you? "))
writeln("In ten years you'll be " + (age + 10) + ".")

A First Constant

Sometimes a value shouldn't be allowed to change once it's set. That's what const is for.

QF Code example 10
const cottageName = "The Hollow Cottage"

A const works like a var you can read but never reassign. Try to change it later, and QF Code stops you with an error. You won't need this often yet, but it's worth knowing it exists. We'll come back to it properly, scope and all, once you've written enough of the game to see why it matters.

Growing the Adventure

Time to make the cottage interactive. Update your program:

QF Code example 11
// The Hollow Cottage
// Chapter 2

const cottageName = "The Hollow Cottage"

writeln("You are standing outside " + cottageName + ".")

var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ". The door is locked.")

Run it. Your game now knows the player's name, and it's already using a constant instead of a hardcoded string.

Make It Yours. Ask the player one more question, something that fits your version of the story, and use their answer in a line of output. Maybe it's their favorite color, a password guess, or the name of their traveling companion. It doesn't need to do anything yet. It just needs to talk back.

Chapter Checkpoint

Open either known-good checkpoint directly in QF Code, or download the .qfc file to keep locally. The IDE opens in a new tab and does not run the program automatically.

Starting code: the Chapter 1 checkpoint.

Completed code (Chapter 2):

QF Code example 12Open in QF Code
// The Hollow Cottage
// Chapter 2

const cottageName = "The Hollow Cottage"

writeln("You are standing outside " + cottageName + ".")

var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ". The door is locked.")

Next: right now, "the door is locked" is just a sentence. Chapter 3 gives your program the ability to actually decide things, so the door can start behaving like a real door.


Copyright © 2026 Edison Mooers. Published by Gibidda Press. Free for personal learning and qualifying noncommercial educational use. Commercial training, organizational use, redistribution, and adaptation require written permission. Full use terms.