Cannot return value from a function: ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or
Cannot return value from a function: ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or
我一直在用 Julia 解决哈佛 CS50 的问题集。这个脚本是我对 [plurality elections.]1
的解决方案
println("How many contenders do we have?")
const max_candidates = parse(Int, readline()) # a maximal number of candidates
# Let us define a composite type for the candidates in our elections
mutable struct Candidate
name::String
votes::Int64
end
function vote(name)
for i in 1:max_candidates
if candidates[i].name == name
candidates[i].votes = candidates[i].votes + 1
end
end
end
function print_winner()
max_votes = 0
for i in 1:max_candidates
if candidates[i].votes > max_votes
max_votes = candidates[i].votes
end
end
for i in 1:max_candidates
if candidates[i].votes == max_votes
candidates[i].name
end
end
end
candidates = Vector{Candidate}(undef, max_candidates)
for i in 1:max_candidates -1
println("Name of the candidate: ?")
name = readline()
votes = 0
candidates[i] = Candidate(name, votes)
println("Thank you, let us move to the next candidate.")
end
#The last candidate i registered outside of the loop because I do no want
#the line println("Thank you, let us move to the next candidate.") to be executed after them.
println("Name of the last candidate: ?")
name = readline()
votes = 0
candidates[max_candidates] = Candidate(name, votes)
println("How many voters do we have?")
voter_count = parse(Int, readline())
for i in 1:voter_count
println("Who are you voting for?")
name = readline()
vote(name)
end
winner = print_winner()
println(winner)
当我 运行 这个脚本时,我得到以下错误
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
错误消息中称为“表达式开始于 /home/jerzy/C.../plurality.jl:65”的表达式是脚本的姓氏。
我不明白这是什么nothing
?
尽管如此,根据错误消息的建议,我修改了代码的最后一行,将其更改为:
println(winner)
至
show(winner)
并得到以下输出:
nothing
我做了一些 research here and there,但作为新手,我不明白为什么我不能 return 我的函数 的值print_winner。根据我的阅读,return 陈述不是强制性的。
在print_winner的定义中,当我代入
candidates[i].name
与
println(candidates[i].name)
然后当最后一行是
winner = print_winner()
然后我终于可以知道获胜者的名字了。但这不是我想要的方式。我想 return 一个值并将其分配给一个变量,然后用这个变量做一些事情。我可以在 PHP 或 Racket 中执行此操作,为什么我不能在 Julia 中执行此操作?
函数 print_winner
没有 return 任何东西,在这种情况下,对象 nothing
实际上是 returned。所以winner
得到值nothing
(从winner = print_winner()
),println(winner)
等同于println(nothing)
,这就导致了错误
I want to return a value and assign it to a variable
然后就这样做:return 来自 print_winner
的值。 Julia 无法知道您希望此函数 return 做什么,因此您必须明确说明。默认情况下,Julia return 是函数表达式的值,在本例中是最后一个表达式的结果,这里是一个 for
循环。 for
循环的表达式值在 Julia 中是 nothing
。
我一直在用 Julia 解决哈佛 CS50 的问题集。这个脚本是我对 [plurality elections.]1
的解决方案println("How many contenders do we have?")
const max_candidates = parse(Int, readline()) # a maximal number of candidates
# Let us define a composite type for the candidates in our elections
mutable struct Candidate
name::String
votes::Int64
end
function vote(name)
for i in 1:max_candidates
if candidates[i].name == name
candidates[i].votes = candidates[i].votes + 1
end
end
end
function print_winner()
max_votes = 0
for i in 1:max_candidates
if candidates[i].votes > max_votes
max_votes = candidates[i].votes
end
end
for i in 1:max_candidates
if candidates[i].votes == max_votes
candidates[i].name
end
end
end
candidates = Vector{Candidate}(undef, max_candidates)
for i in 1:max_candidates -1
println("Name of the candidate: ?")
name = readline()
votes = 0
candidates[i] = Candidate(name, votes)
println("Thank you, let us move to the next candidate.")
end
#The last candidate i registered outside of the loop because I do no want
#the line println("Thank you, let us move to the next candidate.") to be executed after them.
println("Name of the last candidate: ?")
name = readline()
votes = 0
candidates[max_candidates] = Candidate(name, votes)
println("How many voters do we have?")
voter_count = parse(Int, readline())
for i in 1:voter_count
println("Who are you voting for?")
name = readline()
vote(name)
end
winner = print_winner()
println(winner)
当我 运行 这个脚本时,我得到以下错误
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
错误消息中称为“表达式开始于 /home/jerzy/C.../plurality.jl:65”的表达式是脚本的姓氏。
我不明白这是什么nothing
?
尽管如此,根据错误消息的建议,我修改了代码的最后一行,将其更改为:
println(winner)
至
show(winner)
并得到以下输出:
nothing
我做了一些 research here and there,但作为新手,我不明白为什么我不能 return 我的函数 的值print_winner。根据我的阅读,return 陈述不是强制性的。
在print_winner的定义中,当我代入
candidates[i].name
与
println(candidates[i].name)
然后当最后一行是
winner = print_winner()
然后我终于可以知道获胜者的名字了。但这不是我想要的方式。我想 return 一个值并将其分配给一个变量,然后用这个变量做一些事情。我可以在 PHP 或 Racket 中执行此操作,为什么我不能在 Julia 中执行此操作?
函数 print_winner
没有 return 任何东西,在这种情况下,对象 nothing
实际上是 returned。所以winner
得到值nothing
(从winner = print_winner()
),println(winner)
等同于println(nothing)
,这就导致了错误
I want to return a value and assign it to a variable
然后就这样做:return 来自 print_winner
的值。 Julia 无法知道您希望此函数 return 做什么,因此您必须明确说明。默认情况下,Julia return 是函数表达式的值,在本例中是最后一个表达式的结果,这里是一个 for
循环。 for
循环的表达式值在 Julia 中是 nothing
。