gpt4 book ai didi

julia - 无法从函数 : ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show` , `repr` 返回值,或者

转载 作者:行者123 更新时间:2023-12-02 02:46:47 25 4
gpt4 key购买 nike

我一直在和 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 开始的表达式”是脚本的姓氏。我不明白这个什么都没有是什么?尽管如此,根据错误消息的建议,我修改了代码的最后一行,将其更改为:

println(winner)

show(winner)

并得到以下输出:

nothing

我做了一些research在这里和 there ,但作为新手,我不明白为什么我不能从函数返回值print_winner。据我所知,返回语句不是强制性的。

print_winner的定义中,当我替换时

candidates[i].name

println(candidates[i].name)

然后当最后一行是

winner = print_winner()

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

最佳答案

函数print_winner不返回任何内容,在这种情况下,实际上不返回对象nothing。因此,winner 获取值nothing(来自 winner = print_winner()),并且 println(winner) 是等价的到 println(nothing),这会导致错误。

I want to return a value and assign it to a variable

然后就这样做:从 print_winner 返回一个值。 Julia 无法知道您希望此函数返回什么,因此您必须明确说明。默认情况下,Julia 返回函数表达式的值,在本例中是最后一个表达式的结果,这里是一个 for 循环。在 Julia 中,for 循环的表达式值是 nothing

关于julia - 无法从函数 : ERROR: LoadError: ArgumentError: `nothing` should not be printed; use `show` , `repr` 返回值,或者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62658599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com