该脚本非常简单,并且存储在名为001.jl的文件中
sum = 0
for i in 1:10-1
if i%3 == 0 || i%5 == 0
sum = sum + i
end
end
println("The sum is ", sum)
尽管如此,当我尝试在控制台中运行它时,仍然出现此错误
ERROR: LoadError: UndefVarError: sum not defined
Stacktrace:
[1] top-level scope at /Users/aceves/Documents/MyScripts/julia/001.jl:5 [inlined]
[2] top-level scope at ./none:0
[3] include at ./boot.jl:317 [inlined]
[4] include_relative(::Module, ::String) at ./loading.jl:1044
[5] include(::Module, ::String) at ./sysimg.jl:29
[6] include(::String) at ./client.jl:392
[7] top-level scope at none:0
in expression starting at /Users/aceves/Documents/MyScripts/julia/001.jl:3
虽然与julia 0.6完美搭配。如果我在1.0.2版本的REPL中准确键入该内容,它也可以使用。我显然缺少一个细节。有谁知道哪个?
答案 0 :(得分:1)
就像评论中所说的那样,问题在于确定Julia 1.0的变更范围。
应当指出,在诸如此类的许多示例中,最简单,最快的解决方案是将所有代码包含在单个let
块中。 let
块引入了本地范围:
let
sum = 0
for i in 1:10-1
if i%3 == 0 || i%5 == 0
sum = sum + i
end
end
println("The sum is ", sum)
end