3

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
pppery
  • 3,731
  • 22
  • 33
  • 46
katooosha
  • 35
  • 1
  • 1
  • 7

1 Answers1

5

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

Neuron
  • 5,141
  • 5
  • 38
  • 59
Nolin M.
  • 387
  • 1
  • 11
  • Thank you sir! You've fixed the old error message, but a new one is coming up now. The message is "Attempt to compare nil with number". It is on line 3. I have changed the script to what you told me to. Thanks for your help! – katooosha Jul 07 '15 at 12:22
  • 1
    No problem. For the new error however, This error would come up because the variable age is a nil value, Perhaps because it is a string. Add age = tonumber(age) below age = io.read(). I believe this should fix it. – Nolin M. Jul 07 '15 at 12:24
  • Thanks for your further attempt at helping, but I still get the same error message. – katooosha Jul 07 '15 at 12:34
  • Which of the two suggestions of mine did you try? – Nolin M. Jul 07 '15 at 12:35
  • I used the first one (tonumber), as I believe the second one was not what I was wanting my script to be (I want an input box, not a print) – katooosha Jul 07 '15 at 12:37
  • Ok I see. I will write your code into my IDE and test It for errors and try to help you. – Nolin M. Jul 07 '15 at 12:38
  • here is the current script, you're suggestions added in: http://pastebin.com/5LBwJ1ba – katooosha Jul 07 '15 at 12:41
  • What IDE/Program are you using to write lua code in? – Nolin M. Jul 07 '15 at 12:43
  • I am using lExecutor to run it and notepad ++ to write it. – katooosha Jul 07 '15 at 12:44
  • I do not see any issues with the code. Perhaps it is a problem with lExecutor and the io library. You should try to do some research about IExecuter and see If others are having the same problem. – Nolin M. Jul 07 '15 at 12:48
  • Just for confirmation, what exactly does it do? And also, I thank you greatly for taking time to help me :) – katooosha Jul 07 '15 at 12:50
  • No problem, I am always glad to help. When I run your code it prompts me to enter my age. When I enter an age younger than 12 it says "O noes, you are too young!. When I enter an age older than 12 it says "O noes, you are too old!. When I enter 12 it says "Welcome, son!". – Nolin M. Jul 07 '15 at 12:52
  • Yess, it works. I will do some research as to why it doesn't work for me. Once again thanks for your help – katooosha Jul 07 '15 at 12:53