Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I just started using Lua yesterday and figured i'd try basics. What am i doing wrong?

Asked by 6 years ago
print("What is your name?")
name=io.read()
print("Hello " .. name)
hello=io.read()
print("How are you, " .. name)
mood=io.read()
if mood == "bad" then
print("I'm sorry to hear that, " .. name .. ".")
elseif mood == "good" then
print("That's good to hear, " .. name .. ".")
io.read()

it often says: lua: test.lua:12: 'end' expected (to close 'if' at line 7) near '<eof>' but then when i type this

print("What is your name?")
name=io.read()
print("How are you, " .. name .. "?")
mood=io.read()
if mood==good then
    print("That's good to hear, " .. name .. ".")
elseif mood==bad then
    print("I'm sorry to hear that, " .. name .. ".")
io.read()
end

it goes like this:

What is your name? vee How are you, vee? good

and then cuts off :I

0
You forgot quotation marks - "good" "bad" MooMooThalahlah 421 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You should take light of this moment, it will be the first of many problems to solve and learning experiences to be had if you stick with coding!

You did a good job of correcting the problem specified in the error message, however in doing so, you created another (more subtle) problem. It's nice to have the output window tell us when something is wrong with our code, but sometimes we have annoying circumstances where our code isn't working the way we want it to, and the output window just isn't doing anything useful. So how do we deal with problems like this...?

Debugging

The process of locating and correcting a problem in your code is known as debugging. There are many different means of debugging, the most common one being to analyze error messages, but when that isn't working, people often create checkpoints in their code with the print function to tell them what their code is up to. You actually used this method (whether intentionally or unintentionally) to identify and locate where your problem is. Now you ask yourself the more difficult question: "What is causing it?"

Answer

This case its an easy fix. In your first code snippet, you correctly compare two string values together to see if they're equal in your if and elseif statement. In your second snippet, you changed "good" and "bad", in your if statement condition, to good and bad. As subtle of a difference it may be, it is a huge one.

Whenever you have plain text in your script, the interpreter immediately identifies it as a variable, and assumes you set some value to it earlier on in your code. Therefore, you're not actually comparing the text "good" and "bad" in your if statement, you're comparing the variable good and bad which are nil values (any variable not assigned a value defaults to a value called nil in Lua).

Example

print( good ) -- > nil
print( bad ) -- > nil

mood = "good"

-- this compares "good" to nil, resulting in a false condition.
if mood == good then
    ...
end

A quick an easy correction would be to change good and bad back to string values, as they originally where

print("What is your name?")
name = io.read()
print("How are you, " .. name .. "?")
mood = io.read()
if mood == "good" then -- notice "good" is a string value
print("That's good to hear, " .. name .. ".")
elseif mood == "bad" then -- notice "bad" is a string value"
print("I'm sorry to hear that, " .. name .. ".")
io.read()
end

Once these changes are made, you should find the results you're looking for. I hope this helped, just let me know if you have any questions.

Bonus Tip: Clean Code

Just thought I'd add a bonus section that you don't have to read, but it's here if you're interested.

As I said, you don't have to worry about this while you're learning, but it's a good habit to practice early on. Clean code is the practice of organizing your code in such a way that it appears easily readable and visually pleasing to both the author and other potential readers. While the computer doesn't care how your code looks, other human beings do. Clean code isn't just a waste of time "prettying" up your code, it's also very practical. It allows for actions such as debugging and modifying to be done with ease, ultimately saving you a ton of time and stress.

Your code isn't that messy, especially for someone still new to the language, but here is an example of implementing some clean code conventions to your snippet:

local ioRead = io.read

local function prompt(text)
    print(text)
    return ioRead()
end

local userName = prompt("What is your name?")
print("How are you, " .. userName .. "?")

local userMood = prompt("How are you, " .. userName .. "?")

if userMood == "good" then
    print("That's good to hear, " .. userName .. ".")
elseif userMood == "bad" then
    print("I'm sorry to hear that, " .. userName .. ".")
end

Clean code includes creating short but meaningful variable names, proper indentation, use of functions and variables when necessary, etc. There were a lot of adjustments made in the code above, but don't let that intimidate you. It's just there as a guideline that you may return to if you like. Good luck!

0
Thank you so much that fixed the issue VeeMiland 0 — 6y
Ad

Answer this question