'F' → Fullscreen
Copy template-console and rename the copy 1-04-greet. Replace main.lua with this:
io.write("What is your name? ")local name = io.read()
io.write("What is your favorite color? ")local color = io.read()
print("Nice to meet you " .. name .. ".")print("I like " .. color .. " too.")What is your name? Ada [Enter]What is your favorite color? blue [Enter]Nice to meet you Ada.I like blue too.Run it and answer both questions. Then change the questions and the sentences to your own, and add a third question.
Notice the space at the end of "What is your name? ". Take it out and your answer is typed with its nose against the question mark.
Copy the template again to 1-04-age, and write this:
io.write("How old are you? ")local age = io.read()
print("Doubled: " .. age * 2)print("Is it 16? " .. tostring(age == 16))print("Type: " .. type(age))Run it and type 16:
How old are you? 16 [Enter]Doubled: 32Is it 16? falseType: stringCopy all three results onto your sheet. One worked, one was false, and one told you the type was string — even though you typed a number.
Now replace those three lines with a comparison:
io.write("How old are you? ")local age = io.read()
if age > 10 then print("Older than ten.")endHow old are you? 16 [Enter]
Error: main.lua:4: attempt to compare number with string
main.lua:4: in main chunkCopy that message onto your sheet too. Three different outcomes, one typed value.
Put the three print lines back, and change one line:
io.write("How old are you? ")local age = tonumber(io.read())
print("Doubled: " .. age * 2)print("Is it 16? " .. tostring(age == 16))print("Type: " .. type(age))How old are you? 16 [Enter]Doubled: 32Is it 16? trueType: numberPut the comparison back as well and check that it no longer crashes.
Copy the template to 1-04-nextyear and write a program that asks for an age and answers with next year's:
io.write("How old are you? ")local age = tonumber(io.read())
print("Next year you will be " .. age + 1 .. ".")How old are you? 16 [Enter]Next year you will be 17.Then add a second question of your own that does arithmetic on the answer — a number of pets, a jersey number, a locker number. Anything you can add to or multiply.
Run 1-04-nextyear again and type twelve instead of a number:
How old are you? twelve [Enter]
Error: main.lua:4: attempt to perform arithmetic on local 'age' (a nil value)
main.lua:4: in main chunktonumber did not crash. It handed back nil, and line 4 is where somebody finally tried to use it. Write down which line has the mistake and which line reported it.
This is the second time nil has turned up somewhere other than where it was made. It will keep doing that.
1-04-greet asks three questions in your own wording and answers in sentences1-04-age now reports number and true1-04-nextyear does arithmetic on two typed answersAnswer the following questions before submitting your work.
age * 2 gave 32 without tonumber. Explain why that is a problem rather than a convenience.Submit the required files to the appropriate dropbox.