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

Can someone help me fix the errors?

Started by viper0933, 16 December 2013 - 10:44 AM
viper0933 #1
Posted 16 December 2013 - 11:44 AM
Hi to all! I tried to make a simple startup programm, but it has many errors which I cannot fix. Can someone look at the code?
Here it is (I uploaded it from the computer in-game.):
http://pastebin.com/Xzcv3pLa
P.S: Sorry for that, I am new to LUA coding.
Lyqyd #2
Posted 16 December 2013 - 12:23 PM
What are the errors, specifically? What have you done to try to fix them?
Zudo #3
Posted 16 December 2013 - 12:34 PM
sleep(3)

Why?

I can't see anything wrong with it, but…
Edited on 16 December 2013 - 11:34 AM
viper0933 #4
Posted 16 December 2013 - 12:35 PM
What are the errors, specifically? What have you done to try to fix them?
I tryied to change the locations of the "end", but I get the <eof> error. I really don't know why there is this error.
MKlegoman357 #5
Posted 16 December 2013 - 01:14 PM
First problem is that you never declare time variable:


input = time --// Because time is never set to anything your are just setting input to nil

...

sleep(time) --// Sleep requires a number and time is not a number, it is nil

Also, you placed too much end's in a wrong place:


print("Goodbye!")
os.shutdown()
end --// This end shouldn't be here
end --// This end shouldn't be here too
else
print ("Password: Bad, locking for 15 sec.")

--// They should be here:
print ("Password: Bad, locking for 15 sec.")
sleep(15)
os.reboot()
end --// Here
end --// And here

To see if you putted your ends correctly you should indent your code:

Not indented code

local function clear ()
term.clear()
end

for i = 1, 10 do
print(i)
end

x = 20

while x > 10 do
if x > 15 then
x = x - 1.5
else
x = x - 1
end
end

Indented code

local function clear ()
  term.clear()
end

for i = 1, 10 do
  print(i)
end

x = 20

while x > 10 do
  if x > 15 then
      x = x - 1.5
  else
      x = x - 1
  end
end

As you can see, when we indent the code it is easier to see all the ends and where all blocks start and end.
Edited on 16 December 2013 - 12:15 PM
viper0933 #6
Posted 17 December 2013 - 10:16 AM
First problem is that you never declare time variable:


input = time --// Because time is never set to anything your are just setting input to nil

...

sleep(time) --// Sleep requires a number and time is not a number, it is nil

Also, you placed too much end's in a wrong place:


print("Goodbye!")
os.shutdown()
end --// This end shouldn't be here
end --// This end shouldn't be here too
else
print ("Password: Bad, locking for 15 sec.")

--// They should be here:
print ("Password: Bad, locking for 15 sec.")
sleep(15)
os.reboot()
end --// Here
end --// And here

To see if you putted your ends correctly you should indent your code:

Not indented code

local function clear ()
term.clear()
end

for i = 1, 10 do
print(i)
end

x = 20

while x > 10 do
if x > 15 then
x = x - 1.5
else
x = x - 1
end
end

Indented code

local function clear ()
  term.clear()
end

for i = 1, 10 do
  print(i)
end

x = 20

while x > 10 do
  if x > 15 then
	  x = x - 1.5
  else
	  x = x - 1
  end
end

As you can see, when we indent the code it is easier to see all the ends and where all blocks start and end.
THX!!!
But now it doesn't wait for me to input the time.How can I fix it ?
MKlegoman357 #7
Posted 17 December 2013 - 01:28 PM
Remember when you used read function to get the password from the user:


input = read("*")

Just use the same function to get time from the user. But remember - function read returns a string (a sequence of characters). sleep function needs a number. To convert a string that you get from function read you can use a function called tonumber. It is a function that will try to convert a string into a number. So your code should look like this:


print("How much time?")
input = read() --// Ask a user to enter something
input = tonumber(input) --// Convert what they entered into a number
sleep(1)

--// But what if user didn't enter a number? Then your code would fail at this line:

sleep(time) --// Which actually should be this:
sleep(input) --// Because 'input' variable is the one that says how much to wait

--// It would error because tonumber wouldn't return anything, thus setting the variable 'input' to nil
--// So, if user didn't entered a number we can make 'sleep' use a default value, let's say 3

if not tonumber(input) then --// If user didn't enter a number
  input = 3 --// Use a default value, that is 3
else --// If user did enter a number
  input = tonumber(input) --// Use that number that user has entered
end
viper0933 #8
Posted 18 December 2013 - 08:28 AM
Remember when you used read function to get the password from the user:


input = read("*")

Just use the same function to get time from the user. But remember - function read returns a string (a sequence of characters). sleep function needs a number. To convert a string that you get from function read you can use a function called tonumber. It is a function that will try to convert a string into a number. So your code should look like this:


print("How much time?")
input = read() --// Ask a user to enter something
input = tonumber(input) --// Convert what they entered into a number
sleep(1)

--// But what if user didn't enter a number? Then your code would fail at this line:

sleep(time) --// Which actually should be this:
sleep(input) --// Because 'input' variable is the one that says how much to wait

--// It would error because tonumber wouldn't return anything, thus setting the variable 'input' to nil
--// So, if user didn't entered a number we can make 'sleep' use a default value, let's say 3

if not tonumber(input) then --// If user didn't enter a number
  input = 3 --// Use a default value, that is 3
else --// If user did enter a number
  input = tonumber(input) --// Use that number that user has entered
end
THX, it worked!!!