This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
vandbg's profile picture

Error - problem with IF statement or variable?

Started by vandbg, 12 June 2012 - 07:56 PM
vandbg #1
Posted 12 June 2012 - 09:56 PM
I'm getting this error whenever I test this code, but I can't find a syntax problem: bios:206: [string "ProgramName"]:24: 'then' expected.

I suspect it might be because the 'x' on line 8 is undefined if the first 'if' clause returns negative, but I don't know how else to phrase it. I've tried some other forms but I always get this or another 'ambiguous variable' error. As you might have guessed, I'm new to this.


local y=read()

if
type(y) = "number"
then do
x = tonumber(y)
else sleep()
end

if
x>=-1 and x<=37
then
print("How much would you like to place on ", x, "?")
else
print("That is not an option")
end

~~~~

Thanks
OmegaVest #2
Posted 12 June 2012 - 11:01 PM
Okay, so let's format and clear some stuff, shall we?



local y = tonumber(io.read())

if type(y) ~= "number" then
   print("You need to input a number")
else
   if x > -2 and x < 38 then  -- Just makes things a little easier to read, and reduces code. Not really necessary to eliminate the <='s, but in large programs it does help. '
	  print("How much would you like to place on ", x, "?")
   else
	  print("Number too high or too low")  -- Or, whatever
   end
end



Now, I took out that whole "if a number" thing, because if it is a number, it needs not be turned into a number again. Rather, just tell them they did something wrong if it isn't a number in the first place.
Bossman201 #3
Posted 13 June 2012 - 03:56 AM
Your problem is on line 5
then do
It's not proper syntax, just use
then

'Do' is for while loops
while true do
Bossman201 #4
Posted 13 June 2012 - 04:02 AM
Here's your code, formatted nicely with some helpful comments.

local y=read()
if type(y) = "number" then --take out 'do' and put if, then, and everything in-between on one line
 y = tonumber(y) --dont need to make a different variable, this is possible
else
 sleep() --I dont like having code on the same line as 'else', its just preference. Wait, what? sleep()? Are you nuts? Code will have an error
end

if x>=-1 and x<=37 then
 print("How much would you like to place on ", x, "?")
else
 print("That is not an option")
end --Generally format code properly
MysticT #5
Posted 13 June 2012 - 06:04 PM
Some comments on the code, so you can see the errors:

local y=read() -- read() returns a string, it will never be a number.

if type(y) = "number" then do -- you need == to compare, also the do is wrong here
  x = tonumber(y)
else
  sleep() -- sleep requires a number argument, like sleep(1)
end

if x >= -1 and x <= 37 then
  print("How much would you like to place on ", x, "?")
else
  print("That is not an option")
end

Fixed code:


local y = tonumber(read())
if not y then
  print("That's not a number")
  return -- exit the program
end

if y >= -1 and y <= 37 then
  print("How much would you like to place on ", y, "?")
else
  print("That is not an option")
end
OmegaVest #6
Posted 13 June 2012 - 06:25 PM
[left]


local y = tonumber(io.read())

if type(y) ~= "number" then
   print("You need to input a number")
else
   if x > -2 and x < 38 then  -- Just makes things a little easier to read, and reduces code. Not really necessary to eliminate the <='s, but in large programs it does help. '
	  print("How much would you like to place on ", x, "?")
   else
	  print("Number too high or too low")  -- Or, whatever
   end
end

[/left]
[left]

local y = tonumber(read())
if not y then
  print("That's not a number")
  return -- exit the program
end

if y >= -1 and y <= 37 then
  print("How much would you like to place on ", y, "?")
else
  print("That is not an option")
end
[/left]


[left]Mystic, you did it again. :(/>/>[/left]