This is expected, you defined kWsum in a global scope, but the for loop will create its own local variable kWsum by default unless you make it explicitly global. This may look like a weird design decision, but it’s intended to reduce the risk of affecting unintentionally a distant global variable.
You are doing this.
var = 1.0 # global
for i in 1:10
var += 1 # uninitialized new local
end
println(var)
# ERROR: LoadError: UndefVarError: var not defined
# Stacktrace:
# [...]
To solve this the general advice, I think, is to grab all your code in a function (which introduce a local scope). This also have many other advantages (performance included). But if you wanna work in the global scope anyway you can do
var = 1.0 # global
for i in 1:10
global var += 1 # reusing global
end
println(var)
# 11.0
But to understand better I recommend you to read the manual and do a couple of examples.