尝试将nil与数字堆栈回溯进行比较? [英] attempt to compare nil with number stack traceback?

查看:171
本文介绍了尝试将nil与数字堆栈回溯进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下链接与Lua一起玩: https://www.lua.org /pil/4.2.html ,并对此感到困惑.

I'm playing with Lua following this link: https://www.lua.org/pil/4.2.html and get confused about a point.

Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> x=10
> local i=1
> while i<=x do
>>  local x = i*2
>>  print(x)
>>  i=i+1
>> end
stdin:1: attempt to compare nil with number
stack traceback:
    stdin:1: in main chunk
    [C]: in ?

我猜这个错误消息表明表达式while i<=x出了点问题.任何意见,不胜感激. 编辑:我只是意识到这可能是因为它在终端中不起作用.

I guess this error message indicates that something is wrong with the expression while i<=x. Any comments are greatly appreciated. I just realize that it's probably because it does not work in a terminal.

推荐答案

在交互式终端中无法解决.因为一旦按下回车键,终端就会将local i=1本身理解为一个大块.这就是为什么试图将零与数字进行比较"错误的原因;因为未定义i,在这种情况下为nil.要更正它,请将前两行和while循环放入do chuck内,如下所示.

It did not work out in an interactive terminal. Because local i=1 is understood by terminal as a chunk by itself once you hit enter. That is why the "attempt to compare nil with number" error; because i is not defined, i.e., nil in this case. To correct it, put the first two lines and the while loop inside of a do chuck as the following.

> do
>>  x = 10
>>  local i=1
>>  while i<=x do
>>    local x = i*2
>>    print(x)
>>    i = i+1
>>  end
>> end
2
4
6
8
10
12
14
16
18
20
> 

这篇关于尝试将nil与数字堆栈回溯进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆