我使用此getter函数按标签查找乌龟。但现在我需要重构函数以返回其who
值而不是龟本身。我的下面的代码收到以下错误消息。
OF expected input to be a turtle agentset or turtle but got NOBODY instead.
我有以下代码正确执行了海龟查找。
to-report get-user [user-name]
report one-of users with [ label = user-name ]
end
但是当我试图找到那只乌龟的人时,我会继续nobody
。
to-report get-user [user-name]
report [who] of one-of users with [ label = user-name ]
end
有没有更好的方法来找到一个乌龟的人,给定一个字符串并在标签上执行查找?还是我走在正确的轨道上?
答案 0 :(得分:1)
这对我有用没问题 - 你确定你的字符串值匹配吗?如果您输入的值并且该标签不存在,users with [ label = user-name ]
将返回nobody
- 您需要适应该值 - 例如:
to setup
ca
let names [ "John" "Jacob" "Jingle" "Heimer" ]
crt 4 [
setxy ( who + 1 ) * 2 ( who + 1 ) * 2
set label first names
set names but-first names
]
reset-ticks
end
to go
foreach [ "John" "Jacob" "Jingle" "Heimer" "Schmidt" ] [
nm ->
print get-user nm
]
end
to-report get-user [ user-name ]
if any? turtles with [ label = user-name ] [
report [who] of one-of turtles with [ label = user-name ]
]
report "USER DOES NOT EXIST"
end