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

[solved] Don't understand 'while loop' repeating

Started by Andale, 06 January 2013 - 03:58 AM
Andale #1
Posted 06 January 2013 - 04:58 AM
I didn't wanna have to ask questions this soon into the program, but I learned on qBasic so switching to LUA has been rough.
My issue is that my while just keeps repeating. Here is what I think should be happening:

YES = y

while Correct does not = YES do
filler questions
"is this correct?"
local Correct = read()
end
move on

It doesn't move on tho, it just keeps repeating the questions. I assume I'm somehow comparing the YES and Correct incorrectly

There's some extra stuff in here for my learning purposes that you can ignore. I think it is also an issue of me not understanding while loops or the tonumber function. I saw someone say it would be easier to have it just be a number in the first place (maybe the wiki) but I haven't seen how you read() as a number, only to convert with tonumber.

Please look at the code, the example above is the k.i.s.s. version obviously.
Spoiler

YES = ("y")
print ("I'll need the measurements of your hallway.")
while Correct~=YES do
   write ("How TALL? ")
	local Height = read()
	Height = tonumber (Height)
--		while H < 1 do
--		  write ("Must be a number! How TALL? ")
--		  local Height = read()
--		  Height = tonumber (Height)
--		end
   write ("How WIDE? ")
	local Width = read()
	Width = tonumber (Width)
   write ("How LONG? ")
	local Depth = read()
	Depth = tonumber (Depth)
  write ("Ok. I'll make it ") write (Height) write (" tall and ") write (Width) write (" wide and ") write (Depth) print (" long.")
  write ("Is this correct? [y/n] ")
  local Correct = read() print() print(Correct)
end
  print ("I'll get started then. Check back later")
  sleep (2)
Orwell #2
Posted 06 January 2013 - 05:09 AM
Well, there are a couple of things you could improve, but I'm short on time so I'll just point out the error. You have a condition on Correct, but then you define Correct in the local scope of the loop. Basically, the two variables aren't the same. There's a simple and still responsible solution, use a 'repeat … until' loop, this can compare against local variables as well:

YES = ("y")
print ("I'll need the measurements of your hallway.")
repeat
   write ("How TALL? ")
	    local Height = read()
	    Height = tonumber (Height)
--			  while H < 1 do
--			    write ("Must be a number! How TALL? ")
--			    local Height = read()
--			    Height = tonumber (Height)
--			  end
   write ("How WIDE? ")
	    local Width = read()
	    Width = tonumber (Width)
   write ("How LONG? ")
	    local Depth = read()
	    Depth = tonumber (Depth)
  write ("Ok. I'll make it ") write (Height) write (" tall and ") write (Width) write (" wide and ") write (Depth) print (" long.")
  write ("Is this correct? [y/n] ")
  local Correct = read() print() print(Correct)
until Correct == YES
print ("I'll get started then. Check back later")
sleep (2)
remiX #3
Posted 06 January 2013 - 05:10 AM
Have you defined Correct before the loop?
Orwell #4
Posted 06 January 2013 - 05:12 AM
Have you defined Correct before the loop?
That doesn't really matter. The problem is that the variable 'Correct' used in the loop is declared as local within that loop. If he would've defined it before the loop, it would either be a random value and have the same effect, or be "y" and have skipped the loop. But he said it looped for ever, so that's not it.
remiX #5
Posted 06 January 2013 - 05:14 AM
Also, you don't need to use two lines to read the number and then tonumber() it, you can use tonumber(read()).
And the with the part where you use write() multiple times, concatenate the variables:

print ("I'll need the measurements of your hallway.")
repeat -- repeat loop for this kind of thing is always better :)/>
    write ("How TALL?")    local Height = tonumber(read())
--[[    while H < 1 do
        write ("Must be a number! How TALL? ")
        local Height = read()
        Height = tonumber (Height)
        end
]]
    write ("How WIDE? ") local Width = tonumber(read())
    write ("How LONG? ") local Depth = tonumber(read())
    write ("Ok. I'll make it " .. Height .. " tall and " .. Width .. " wide and " .. Depth .. " long.") -- Instead of using write the whole time, concatenate the strings with double dots
    write ("Is this correct? [y/n] ")
    local Correct = read() print() print(Correct)
until Correct == "y" -- you don't need to use a variable (this is easier) but you can if you want
-- repeat loop needs an until condition like the one above.

print ("I'll get started then. Check back later")
sleep (2)
Andale #6
Posted 06 January 2013 - 05:40 AM
This is great guys. Thanks so much. All this info while i went and showered lol.

Ok, i didn't realize that 'local' inside a 'while' loop would only be defined in that group. Thanks for that info. Do my H/W/D continue to be defined outside the loop? (I will change it to the 'repeat-until' function.) Can I define as something other than 'local' to make it remember that value outside the loop if I were to want to use the 'while' in this fashion. I don't see 'global' as a command and I grew up on qBasic so I'm used to being able to define something and it be that anywhere.
Thanks so much for "tonumber(read())". That will really clean up some stuff. Also the write function is great. I saw that in the tunnel program but I didn't pick up on it. I assume that works with print as well?

I bet you guys have posted like 3 times since I started this post :)/>

OH! I think this also answers why my 'check for number' function was borked.

I also note (after rereading thru the wiki) that I could have used a global by just putting 'this = that' instead of using local, but that it is not a good idea because it is global to the OS on that server, so global can mess up other programs.