luagarrys-mod

Unexpected symbol near '<'


I'm trying to code something in Lua where you must be exactly 12 years old to make it print "Welcome!". However, whenever I run this code, I get an error message saying

Unexpected symbol near '<'.

The error message says that this is on line 3. If possible, could anyone also point out the other potential errors in this code? My code looks as follows:

io.write ("Enter your age:")
    age = io.read()
if age == <12 then
    print ("O noes, you are too young!")
elseif age == >12 then
    print ("O noes, you are too old!")
else 
    print ("Welcome, son!")
end

Solution

  • You have Unnecessary =='s.

    Change the code to:

    io.write ("Enter your age:")
    age = io.read()
    if age < 12 then
        print ("O noes, you are too young!")
    elseif age > 12 then
        print ("O noes, you are too old!")
    else 
        print ("Welcome, son!")
    end
    

    When you are checking if a variable is greater than or less than another variable, you do not need ==.

    Example: if (7 < 10) then if (9 > 3) then


    This may also be helpful:

    As this is your first Lua code, It may also be helpful to note that if you are checking if a variable is greater than or equal to (or less than or equal to) you would need to write it as if (5 >= 5) then or if (3 <= 3) then.

    You only need == when you are ONLY checking if it is equal to the other variable.

    Example: if (7 == 7) then