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

While loop not working as expected

Started by Zinx, 21 August 2016 - 01:29 AM
Zinx #1
Posted 21 August 2016 - 03:29 AM
I have some experience coding, but not so much with Lua so I'm sure my issue is just a small nuance that I am missing that is particular to Lua. All I'm trying to do is wait for a user to put in a correct value for a choice (1 or 2) and loop the question until they put in the right response. What the code is doing instead is infinitely looping even when the value being checked is correct. I've tested my code logic out in C# and it works as expected, with Lua however, it seems to be broken. I'm playing the FTB Infinity pack. Please help me see what I'm missing if you could. Thanks.


number = 5
while number~=1 or number~=2 do
	print("choose 1 or 2")
	print("number is: " .. number)
	number = read()
	print("number is now: " .. number)
end

print("out of loop")
read() --for pause to check output
Lyqyd #2
Posted 21 August 2016 - 06:00 AM
One of your two conditions will always be true, since number cannot be equal to both 1 and 2 at the same time. You'll want to replace the or with an and. You'll also need to tonumber the result of the read call.
Zinx #3
Posted 21 August 2016 - 09:01 AM
Lol, well, that is a bit embarrassing. That is what I get for coding while tired. Let that be a lesson to you, don't code on a poor nights sleep. I appreciate the extra pair of eyes, thank you.