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

A While in a While Question

Started by rollerman109, 19 December 2012 - 11:35 PM
rollerman109 #1
Posted 20 December 2012 - 12:35 AM
Whenever i try making a while in a while question with number inputs from a read() it always turns out with

attempt to compare string with number expected, got string

This is an example of one of the codes i was using, and i am using Tekkit 1.1.1 as well, if that makes a difference.

write("Please insert how TALL you want the wall: ")
H = read()
write("Please insert how LONG you want the wall:")
L = read()
write("The turtle will need ")
write(H*L)
print("blocks of whatever material")
print(" ")
print("Once this turtle has been filled with material, type OK and press Enter, type anything else or just press Enter to cancel")
if a == "OK" then
B = L
C = H
while B > 0 do
while C > 0 do
turtle.forward()
turtle.placeDown()
B = B - 1
end
B = L
turtle.up()
turtle.turnRight()
turtle.turnRight()
C = C - 1
write("Layer ")
write(C)
write("of ")
print(H)
end
end
print("Thank you for using Rollerman's Turtle Program")

What i decipher is that i doesn't recognize that B is a number so it cant compare it to 0, but the number works where it is multiplied, so i am stumped as to what causes the problem.
Kingdaro #2
Posted 20 December 2012 - 12:56 AM


Dafuuuuu.

But that's beside the point. To compare a string input against another number, you need to convert the string to a number.


H = tonumber(read())
L = tonumber(read())
rollerman109 #3
Posted 20 December 2012 - 01:00 AM
Sorry about formatting, I couldn't press tab when inserting the code…

I'm trying this now…
remiX #4
Posted 20 December 2012 - 01:01 AM
Try using for loops rather:
for i = 1, H do ... end
I'll fix it now and show you how it works

This works perfectly, I made it use arguments rather (actually easier ;)/>):

Spoiler

Args = {...}
H = tonumber(Args[1])
L = tonumber(Args[2])
if #Args ~= 2 or not H or not L then
    print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <height> <length>")
    return
end
turtle.select(2)
turtle.refuel()
turtle.select(1)
print("The turtle will need " .. H*L .. " blocks of whatever material.")
print("Once this turtle has been filled with material, type OK and press Enter, type anything else or just press Enter to cancel")
a = read()
if a == "OK" then
    for j = 1, L do
        for i = 1, H do
            turtle.place()
            if i ~= H then turtle.up() end
        end
        for i = 1, H do
            turtle.down()
        end
        turtle.turnRight()
        turtle.forward()
        turtle.turnLeft()
    end
end

-- Return to start
turtle.turnLeft()
for i = 1, L do
    turtle.forward()
end
turtle.turnRight()
print("Thank you for using Rollerman's Turtle Program")
rollerman109 #5
Posted 20 December 2012 - 01:19 AM
1. I have troubles understanding half of that
Like where is the question of how high and long, i just see it wanting variables from the start…
2. This tekkit version is before it needs fuel
3. I only half know how to get programs off the web without downloading them

Notice the title that i have.
rollerman109 #6
Posted 20 December 2012 - 01:42 AM
It works. but the ground has to be flat otherwise it goes a little bit lower than it should

Modified it slightly to my needs. Works as intended. No bugs found.
remiX #7
Posted 20 December 2012 - 02:14 AM
Yeah well that's what I thought you wanted :P/> It uses arguments so you run it like:
wall <height> <length>
rollerman109 #8
Posted 20 December 2012 - 02:20 AM
when i ran it, the height and length were the wrong way round.
and what i wanted (because everyone is picky) is the turtle to add layers onto it, so if i feel like it, maybe add a width onto the dimensions. And personal touches, you need personal touches.
But dont add width now. unless u really want to. =P

The modified code.

Args = {...}
H = tonumber(Args[1])
L = tonumber(Args[2])
if #Args ~= 2 or not H or not L then
    print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <length> <height>")
    return
end
turtle.select(2)
turtle.select(1)
print("The turtle will need " .. H*L .. " blocks of whatever material.")
print("Once this turtle has been filled with material")
print("Type OK and press Enter to continue")
print("Type anything else or just press Enter to cancel")
a = read()
if a == "OK" then
    for j = 1, L do
	    for i = 1, H do
		    turtle.placeDown()
		    if i ~= H then turtle.forward() end
	    end
	    turtle.turnRight()
	    turtle.turnRight()
	    turtle.up()
    end
end

sleep(3)
print("Thank you for using Rollerman's Turtle Program")
sleep(3)
print("It will now dance for you")
shell.run("dance")
remiX #9
Posted 20 December 2012 - 03:32 AM
Oh a wall as in on both sides? But the code you posted will skip a block because after you go turn around it will go just go up?
Lyqyd #10
Posted 20 December 2012 - 03:46 AM


Dafuuuuu.

But that's beside the point. To compare a string input against another number, you need to convert the string to a number.


H = tonumber(read())
L = tonumber(read())

Arithmetic operations have a habit of tonumber-ing strings.
rollerman109 #11
Posted 20 December 2012 - 10:20 AM
It seems to work perfectly fine.
I told it to build a 10 x 10 wall, it makes a nice flat wall. I see no holes in the wall.
The turtle just has to be placed one block higher than the bottom of the wall as it places beneath it.


That explains why it did the sum but not the questions, thank you.