Julia 无法从函数返回值:ERROR:LoadError:ArgumentError:'nothing'不应打印;使用“show”、“repr”或

Julia 无法从函数返回值:ERROR:LoadError:ArgumentError:'nothing'不应打印;使用“show”、“repr”或,julia,void,Julia,Void,我一直在和朱莉娅一起解决哈佛CS50的习题集。此脚本是我的[]解决方案 当我运行这个脚本时,我得到以下错误 ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead. Stacktrace: [1] print(::Base.TTY, ::Nothing) at ./show.jl:566 [2] print(::Base.TTY

我一直在和朱莉娅一起解决哈佛CS50的习题集。此脚本是我的[]解决方案

当我运行这个脚本时,我得到以下错误

ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.
Stacktrace:
 [1] print(::Base.TTY, ::Nothing) at ./show.jl:566
 [2] print(::Base.TTY, ::Nothing, ::Char) at ./strings/io.jl:42
 [3] println(::Base.TTY, ::Nothing) at ./strings/io.jl:69
 [4] println(::Nothing) at ./coreio.jl:4
 [5] top-level scope at none:0
 [6] include at ./boot.jl:317 [inlined]
 [7] include_relative(::Module, ::String) at ./loading.jl:1044
 [8] include(::Module, ::String) at ./sysimg.jl:29
 [9] exec_options(::Base.JLOptions) at ./client.jl:266
 [10] _start() at ./client.jl:425
in expression starting at /home/jerzy/C.../plurality.jl:65
错误消息中称为“expression start at/home/jerzy/C../multiplex.jl:65”的表达式是脚本的姓氏。 我不明白这是什么什么? 然而,根据错误消息的建议,我修改了代码的最后一行,将其从:

println(winner)

并得到以下输出:

没什么

我在这里做了一些尝试,但作为一名新手,我不明白为什么不能从函数print\u winner返回值。从我所读到的,返回语句不是强制性的

在打印优胜者的定义中,当我替换

candidates[i].name

最后一行是什么时候

winner = print_winner()

然后我终于可以知道获胜者的名字了。但这不是我想要的方式。我想返回一个值并将其赋给一个变量,然后使用这个变量做一些事情。我可以在PHP或Racket中执行此操作,为什么不能在Julia中执行此操作?

函数
print\u winner
不返回任何内容,在这种情况下,实际上返回的是对象
nothing
。因此
winner
获取值
nothing
(来自
winner=print\u winner()
),而
println(winner)
相当于
println(nothing)
,这导致了错误

我想返回一个值并将其赋给一个变量

然后就这样做:从
print\u winner
返回一个值。Julia不知道您希望这个函数返回什么,所以您必须明确说明它。默认情况下,Julia返回函数表达式的值,在本例中,该值是最后一个表达式的结果,这里是
for
循环。在Julia中,-loop的
表达式值为
nothing

这在1.3中更改,请参阅。但是你能考虑一下改变你的头衔吗?实际的问题在于返回值,而不是什么都不打印。将“候选者[i].name”替换为“返回候选者[i].name”解决了这个问题。
candidates[i].name
println(candidates[i].name)
winner = print_winner()