小编phr*_*iol的帖子

Lua脚本抛出错误"尝试调用零值(字段'存款')"

我有这个Lua脚本,它应该创建一个新类,创建一个实例并调用函数,但是我实际上调用了这些方法时出现了错误.

Account = {
    balance = 0,
    new = function(self,o)
        o = o or {}
        setmetatable(o,self)
        self.__index = self
        return o
    end,
    deposit = function(self,money)
        self.balance = self.balance +  money
    end,
    withdraw = function(self,money)
        self.balance = self.balance - money
    end

}
new_account = Account.new()
print(new_account.balance)
new_account.deposit(23)
new_account.deposit(1)
print(new_account.balance)
Run Code Online (Sandbox Code Playgroud)

它不断抛出这个错误:

attempt to call a nil value (field 'deposit') 
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

Account = {
    balance = 0,
}

function Account:new(o)
    o = o or {}
    setmetatable(o,self)
    self.__index = self
    return o
end

function …
Run Code Online (Sandbox Code Playgroud)

oop scripting lua operators colon

4
推荐指数
1
解决办法
1万
查看次数

如果语句语法错误

我有这段代码应该写文件:

with open('key.txt', 'w+') as key:
       counter += 1
       key.write(k + str(counter)
       contents = key.read()
       if contents == 'ran 1':
           print('Can\'t run twice!')
       else:
           writeFiles()
Run Code Online (Sandbox Code Playgroud)

我的Python编译器一直在抱怨并抛出这个错误:

File "/home/ubuntu/workspace/fun.py", line 30                                                                                                                                                                                        
    if key.read() == 'ran: 1':                                                                                                                                                                                                         
                             ^                                                                                                                                                                                                         
SyntaxError: invalid syntax    
Run Code Online (Sandbox Code Playgroud)

我真的不明白这意味着什么,我通常不会得到这样的错误.帮助将不胜感激.

python file syntax-error

0
推荐指数
1
解决办法
80
查看次数

标签 统计

colon ×1

file ×1

lua ×1

oop ×1

operators ×1

python ×1

scripting ×1

syntax-error ×1