07. Hello, World — Homework solutions
Try every problem on your own first. The homework is about the struggle, not the answer. If a problem won't work, read its How to think about it section and try again before looking at the worked solution.
The .lua solution files live in
exercises/07/homework/solutions/. The walkthroughs below
explain why each solution looks the way it does.
Problem 1 — Greet yourself
Problem. Print a greeting, your first name, and your favourite game on three separate lines.
How to think about it. print writes one
line per call. Three lines means three print calls; call
order is line order on screen.
Worked solution.
print("Hello there!")
print("Keiko")
print("Roblox")When run, the terminal prints:
Hello there!
Keiko
Roblox
Common mistakes.
- Putting all three things in one
printcall separated by commas. That works, but everything lands on one line with tabs between, not three lines. - Forgetting the quotes.
print(Hello there!)is an error: without quotes Lua readsHelloandthereas variable names, not text.
Problem 2 — Print a quote
Problem. Print a quote on the first line and the author on the second line.
How to think about it. Two print calls.
The first prints the quote (with its double quotes as part of the text);
the second prints the author with a dash in front.
One subtlety: the quote itself contains double quotes
("). If you also wrap the whole string in double quotes,
Lua loses track of where the string ends. Two ways out:
- Wrap the outer string in single quotes:
'"quote here"'. - Keep double quotes outside and use
\"(a backslash before the inner quote) so Lua knows that quote is part of the text.
The first option is simpler. Use it.
Worked solution.
print('"The best way to predict the future is to invent it."')
print(" - Alan Kay")The leading spaces in the second print push the author
near the right side of the quote.
Common mistakes.
- Writing
print(""The best way...""). The first""is an empty string, thenTheis read as a variable name, and Lua complains. - Using smart quotes (
“ ”) instead of straight quotes ("). Word processors insert these; code editors do not. VS Code is fine, but double-check text copied from a website.
Problem 3 — Fix the error
Problem. A starter file has more than one mistake.
Make it run and print All good now.
How to think about it. Run the file. Read the first error. Fix exactly what it mentions. Run again, read the next error, and repeat until the file runs.
Do not try to spot every mistake by eye first. Errors are free hints.
Worked solution. The starter file looks like this:
Print("Halfway there)
prnt('All good now.'There are four problems:
Printshould beprint(Lua is case sensitive).- The first string is missing its closing
". prntis not a real function. The line should callprint.- The last line is missing its closing
).
A working version:
print("Halfway there")
print("All good now.")(Keep both print lines or delete the first. As long as
the file runs and the last output line is All good now., it
counts.)
Common mistakes.
- Fixing the first line but not running again before checking the second. The second error is sometimes a side effect of the first, so running each time keeps the feedback loop tight.
- Fixing
prntby inventing some other function. Lua only knows the names it knows. When in doubt, type the name slowly and check it against the book.
Challenge — Three-line story
Problem. Write a three line micro-story (setup, problem, resolution) with a multi-line comment at the top explaining the story.
How to think about it. Two ingredients you already
know: --[[ ]] for a multi-line comment and
print for output. Sketch the story in plain English first,
then turn each line into a print call.
Worked solution.
--[[
A small story about a player who finds a chest in a cave,
realises it is locked, and figures out how to open it.
]]
print("A lone player walked into the cave and saw a chest.")
print("The chest was locked, and the key was nowhere in sight.")
print("She remembered the riddle on the wall and spoke the password.")Common mistakes.
- Forgetting the closing
]]. Then everything after--[[counts as comment, including yourprintcalls, and the program prints nothing. - Putting
--[[in the middle of a code line. It works, but it is confusing. Keep multi-line comments on their own lines for now.
Done?
If every problem ran cleanly, you have finished your first chapter. The next three chapters round out the terminal basics: comments for leaving notes, the print toolkit for showing things exactly how you want, and reading error messages for when something goes wrong.