Congratulations!

Congratulations, your program works, and you've reached the end of the Lua course! Emma started the season with 30 coins and planted three plots. Whenever Hugo paid more than the market, she sold him her harvest, and the rest went to the market. She ended the season with 174 coins. And you wrote every line of it yourself.

One last improvement. Emma wants to try a new crop: tomatoes. In your program, that means two new lines of data, a record in crops and an item in plan, and nothing else. The price list, the planting, the growing, the barn, Hugo and the market all work with whatever is in the lists, so the tomatoes find their place everywhere by themselves. Watch the coins too: the tomato seeds cost exactly the 6 coins Emma has left, and thanks to <=, she can buy them.

What you've learned. Look at how each lesson of the course found its place in your program:

  • Lua Basics: print, a program that runs from top to bottom, and comments that split it into parts. When something went wrong, the error message told you on which line.
  • Variables and Types: local variables such as coins, and whole numbers, which %d prints. With a decimal number such as 3.5, %d would end with an error.
  • Conditions decide whether Emma has enough coins, whether a plot is ripe, whether Hugo pays more than the market and whether there's anything to print.
  • Loops go through the plan, the days and the plots, a loop in a loop. for extra = 2, 0, -1 counts down, and the while true in Hugo's script isn't endless, because it pauses at every yield.
  • Tables as Lists hold the crops, the plan, the plots and the weather. table.insert adds to them, #plots counts them, table.concat joins texts and table.sort sorts them.
  • Functions split the program into parts with clear jobs: findCrop searches, sortedNames sorts, and sell prints and returns the coins. Its default parameters kept the old call sell(barn) working.
  • Tables as Dictionaries: each crop is a record, growth[today] looks up the weather, and the barn counts the crops by name. Because pairs has no order, the keys get sorted before they're printed.
  • Strings: string.format builds the lines with %s and %d, and .. joins the rest.
  • Objects and Metatables: the class Plot keeps a plot's data and methods together. __index finds the methods, plot:grow() passes the plot as self, and __tostring turns it into carrot 2/3.
  • Coroutines: Hugo's script pauses at coroutine.yield and continues the next day, in the middle of its loop, with its variables as they were.

Lua on your own computer. Here, your browser ran the programs. To run them elsewhere, you have several options:

  • Online, without installing anything: the official demo at lua.org/demo.html lets you try Lua 5.4 right from your browser. Paste your program in and run it.
  • macOS: with Homebrew, install Lua with brew install lua in the Terminal. Save your program as main.lua, and in its folder, run it with lua main.lua.
  • Ubuntu and Debian Linux: install Lua with sudo apt install lua5.4, and run your program with lua5.4 main.lua.
  • Windows: download ready-made Lua from LuaBinaries. The program is called lua54.exe, so you run your program with lua54 main.lua.

Write your programs in an editor that understands Lua, for example Visual Studio Code with the extension called “Lua” by sumneko (the Lua Language Server). It marks mistakes as you type, before you even run the program.

The error messages are the ones you know from here, with two small differences: the first line starts with the command's name, such as lua: , and the traceback ends with [C]: in ?. For example, when the plan has a typo, lua main.lua prints this:

lua: main.lua:101: attempt to index a nil value (local 'crop')
stack traceback:
	main.lua:101: in main chunk
	[C]: in ?

Other Luas. You've learned standard Lua 5.4. You'll also meet its relatives:

  • Luau, the language of Roblox, grew out of Lua 5.1. It has things that standard Lua doesn't: coins += 5 and the like, continue in loops, texts with values in backticks, `Coins: {coins}`, and optional types, local coins: number = 5. All its numbers are decimal numbers, so print(10 / 2) prints 5. And Roblox games use Roblox's own objects, such as game, workspace and events, on top of the language you've learned.
  • LuaJIT is a very fast Lua 5.1, used by the LÖVE game engine, by Defold and by Neovim. Lua 5.1 has no // and doesn't split numbers into whole and decimal ones.
  • World of Warcraft add-ons use Lua 5.1, and PICO-8 uses a small language similar to Lua.

What's next. You now know Lua from print all the way to metatables and coroutines. Here are some ideas for what to do next:

  • Keep improving your program. Grandpa Joe could be a second trader with a coroutine of his own, and Emma could sell to whoever pays more. The season could be longer, a plot could get a method plot:isReady(), and on your own computer, the weather can be random with math.random: there, no test needs the same output every time. You learn the most by solving your own problems.
  • Games with LÖVE. LÖVE is a free engine for 2D games, and you write the whole game in Lua: what happens in every frame, what's drawn and what a key press does.
  • Roblox Studio. Roblox games are written in Luau, in Roblox Studio. Almost everything from this course works there, and you'll add Roblox's objects and events to it.
  • Neovim. The text editor Neovim is set up and extended in Lua, so you can now write your own settings and plugins for it.
  • The Lua manual. The Lua 5.4 Reference Manual describes the whole language and all its functions (in English). And the book Programming in Lua by Roberto Ierusalimschy, one of Lua's authors, explains it in depth.

And if you'd like to try a different language, the Python, JavaScript and C courses are waiting for you.

Instructions

Add tomatoes at the end of crops: {name = "tomato", days = 4, seedPrice = 2, price = 7}. And at the end of plan, Emma wants 3 tomato seeds: {name = "tomato", seeds = 3}. Don't change anything else. The program then prints everything from the beginning of the lesson:

PRICE LIST
carrot: seed 2, sells for 5
pumpkin: seed 5, sells for 20
strawberry: seed 4, sells for 12
wheat: seed 1, sells for 3
tomato: seed 2, sells for 7
Plot 1: carrot x4 for 8 coins
Plot 2: wheat x6 for 6 coins
Plot 3: pumpkin x2 for 10 coins
Not enough coins for strawberry x2
Plot 4: tomato x3 for 6 coins
Coins: 0
Day 1 (sunny): carrot 1/3, wheat 1/2, pumpkin 1/6, tomato 1/4
Day 2 (sunny): carrot 2/3, wheat 2/2, pumpkin 2/6, tomato 2/4
Hugo: wheat 6 x 4 = 24
Day 3 (rainy): carrot 3/3, wheat 2/2, pumpkin 4/6, tomato 4/4
Day 4 (cloudy): carrot 1/3, wheat 1/2, pumpkin 5/6, tomato 1/4
Hugo: carrot 4 x 7, tomato 3 x 9, wheat 6 x 5 = 85
Day 5 (sunny): carrot 2/3, wheat 2/2, pumpkin 6/6, tomato 2/4
Hugo: pumpkin 2 x 21, wheat 6 x 4 = 66
Day 6 (cloudy): carrot 3/3, wheat 1/2, pumpkin 1/6, tomato 3/4
Market: carrot 4 x 5 = 20
End of the season: 195 coins

If the tomatoes don't get a plot, check the condition for buying the seeds: Emma has exactly 6 coins left, and that's enough.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

7/7