Printing
A program can have as many commands as you need. Lua carries them out one after another, from top to bottom, and each print prints a new line:
print("Good morning, Emma!")
print("The hens laid 7 eggs.")
print("Bella gave 12 liters of milk.")This prints:
Good morning, Emma!
The hens laid 7 eggs.
Bella gave 12 liters of milk.The lines in the output come in the same order as the commands in the program. When you swap two prints, their lines swap too.
A print() with nothing in the parentheses prints an empty line. It's handy for separating parts of the output:
print("Monday")
print()
print("Weather: sunny")This prints:
Monday
Weather: sunnyA text can be written in double quotes, "Hello", or in single quotes, 'Hello'. Both mean the same, as long as the text starts and ends with the same kind of quote. In this course, we use double quotes. The other kind comes in handy when the text itself contains a quote or an apostrophe:
print("Emma's farm")
print('Rex says "Woof!"')This prints:
Emma's farm
Rex says "Woof!"In single quotes, print('Emma's farm') doesn't work: Lua thinks that the text ends at the apostrophe, then finds an s that makes no sense to it, and the program ends with an error. The same happens when the quotes at the start and at the end of a text differ, as in print("Hello'): Lua can't tell where the text ends.
Watch out for missing quotes, too. Without them, Lua doesn't read words as a text, but as names, such as the name of the function print:
print(Good morning)ends with an error, because two names in a row make no sense to Lua.- A single word without quotes doesn't even cause an error:
print(Carrot)printsnil. Nothing in the program is calledCarrot, andnilis Lua's word for “nothing”. You'll learn more about names andnilin the next lesson.
Instructions
The program in the editor prints Emma's to-do list, but its heading is in the wrong place. Run it, and then move the line that prints To-do list for Monday to the top, so that the program prints:
To-do list for Monday
Feed the hens
Milk BellaAt the end of the list, add two more tasks, each with its own print. The second one contains an apostrophe, so think about which quotes to use. The program then prints:
To-do list for Monday
Feed the hens
Milk Bella
Water the carrots
Fill Rex's bowlFinally, below the list, print an empty line and the weather, so that the program prints:
To-do list for Monday
Feed the hens
Milk Bella
Water the carrots
Fill Rex's bowl
Weather: sunnyStart programming for free
2/7