无法正确执行if语句

时间:2013-09-18 21:49:40

标签: if-statement lua

这已经困扰了我很长一段时间,我一直在制作一个程序,如果你键入一个密码就可以简单地执行一个动作。我一直收到错误说“:4:''预期或”如果声明预期“请帮忙!

 textutils.SlowPrint("type the password")
--This is a W.I.P and it is in a basic stage!
pass = ImCool
setCursorPos.(4,5)
input.read(*)
--problem is here
if input = pass then
print("You did it"!)

2 个答案:

答案 0 :(得分:1)

当您尝试检查相等性时,请使用==运算符,如下所示:

if input == pass then
    print("You did it")
end

您现在正在使用=

您的代码也有其他拼写错误 - .之后setCursorPos!超出双引号。 ImCool - 这是一个符号还是一个字符串?如果它是一个字符串,它应该是双引号。

我不确定那条input.read(*)行 - 它似乎不对,但我还没有使用Lua一段时间。我记得使用io.read()在Lua中获取命令行输入。

答案 1 :(得分:0)

我不是lua专家,但是当您将ImCool分配给pass时,看起来应该在pass = "ImCool" 附近有引号:

end

我知道你的问题发生在后来的代码行中,但是像这样的错误可能会破坏解析器。

查看http://lua-users.org/wiki/LuaTypesTutorial处的字符串部分以初始化字符串变量。

另外,我不知道你复制代码时它是否被切断,但你的if语句后可能需要==。你也可能想在if语句中使用=运算符而不是{{1}}。有关详细信息,请参阅http://www.lua.org/pil/4.3.1.html