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

stuck in a loop...loop...loop...loop

Started by Escarchado, 19 October 2014 - 06:59 PM
Escarchado #1
Posted 19 October 2014 - 08:59 PM
This keeps looping back to the second print statement.



function tunnelDis()   
local i = 0
    repeat
    io.flush()
    i = i + 1
   
    if i == 1 then
        io.write("Enter a length for the tunnel (10 to 100).")
        dis = io.read()

    elseif i > 1 then
        print("Please enter a number from 10 to 100.")
        dis = io.read()
       
    end
   
    until dis == "10,100"
   
    print("Ok, the tunnel length will be (dis).")
       
end
Lyqyd #2
Posted 19 October 2014 - 10:28 PM
It will loop forever until you enter "10,100" as the input text. You'd need to tonumber the result of the read call and then compare it with a comparison something like this:


until dis >= 10 and dis <= 100
Escarchado #3
Posted 20 October 2014 - 01:25 AM
It will loop forever until you enter "10,100" as the input text. You'd need to tonumber the result of the read call and then compare it with a comparison something like this:


until dis >= 10 and dis <= 100
ok, that gave me an unexpected symbol error on the new line.
I changed it to this and it went away.

until dis > 9 and dis < 101
now I'm getting attempt to compare string with number expected, got string.
Edited on 19 October 2014 - 11:29 PM
valithor #4
Posted 20 October 2014 - 03:46 AM
It will loop forever until you enter "10,100" as the input text. You'd need to tonumber the result of the read call and then compare it with a comparison something like this:


until dis >= 10 and dis <= 100
ok, that gave me an unexpected symbol error on the new line.
I changed it to this and it went away.

until dis > 9 and dis < 101
now I'm getting attempt to compare string with number expected, got string.

io.read() returns a string use dis = tonumber(io.read())
that will convert it from a string to a number when it is entered

It should not have given you a unexpected symbol error if you entered it correctly. The = has to go on the right side of the < or >. If it is on the wrong side you will get the unexpected symbol error.
Edited on 20 October 2014 - 01:49 AM
Escarchado #5
Posted 20 October 2014 - 03:01 PM
It will loop forever until you enter "10,100" as the input text. You'd need to tonumber the result of the read call and then compare it with a comparison something like this:


until dis >= 10 and dis <= 100
ok, that gave me an unexpected symbol error on the new line.
I changed it to this and it went away.

until dis > 9 and dis < 101
now I'm getting attempt to compare string with number expected, got string.

io.read() returns a string use dis = tonumber(io.read())
that will convert it from a string to a number when it is entered

It should not have given you a unexpected symbol error if you entered it correctly. The = has to go on the right side of the < or >. If it is on the wrong side you will get the unexpected symbol error.
That's fixed it! Thx. I changed the "<= , >=" symbols back and that's good also.