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

[Solved] [Lua] loops not ending

Started by jonteman98, 12 February 2013 - 09:39 AM
jonteman98 #1
Posted 12 February 2013 - 10:39 AM
i are trying to end a loop with y or Y.
the bit of the code i that are not ending:

local xp1 = "y"
local xp2 = "y"
local stuff1 = read()

if stuff1 == "tw" then
while xp1 == "y" or "Y" do
rsToggle() – a function longer upp in the code
print("water toggled")
print(" toggle again? (Y/N)")
xp1 = read()
end –here
elseif stuff1 == "xp" then
while xp2 == "y" "Y" do
xpFarm() – a function
xp2 = read()
end –and here
end

edit: how do you use code tags?
LBPHacker #2
Posted 12 February 2013 - 11:16 AM
Use
 while xp1 == "y" or xp1 == "Y" do 
or
 while string.lower(xp1) == "y" do 
instead of
 while xp1 == "y" or "Y" do 

It won't stop looping because (xp1 == "y" or "Y") is always true. "Y" is true, since it's not nil or false.

EDIT: Thanks ChunLing!

EDIT2: Code tags?
[code]
Code here
[/code]
ChunLing #3
Posted 12 February 2013 - 11:19 AM
The loop never ends because your conditional parses as (xp1 == "y") or "Y", and "Y" is never nil or false. Use xp1 == "y" or xp1 == "Y" instead.

Ninja'd, but I get to point out that ("Y" == true) is false rather than true.
Edited on 12 February 2013 - 10:20 AM
jonteman98 #4
Posted 13 February 2013 - 10:15 AM
thanks!