How to resolve the error: ITEM expected input to be a number but got the string "12" instead in NetLogo?
How to resolve the error: ITEM expected input to be a number but got the string "12" instead in NetLogo?
我有一个列表,我想删除空格和方括号并将其转换为数字以在代码中的其他地方使用。
turtle-profiles-habitat[ [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] ]...
turtle-profiles-habitat-code [ 1 2 3 4 5 12 13 14 15 23 ]...
但是,出现以下错误:ITEM 预期输入的是数字,但得到的却是字符串“12”。
我可以进行字符串到数字的转换吗?
提前致谢
下面的代码
globals [ ValidHabs ]
turtles-own [ turtle-profiles-habitat-code turtle-profiles-habitat t-p-h-item-ValidHabs my-patches ]
patches-own [ habitatcover ]
to setup
ca
set ValidHabs [[1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 3 4 5] [1 2 4 5] [1 2 3 5] [2 3 4 5] [1 2 3 4 5]]
(
foreach ValidHabs [
this-profile ->
ask one-of patches [
sprout 1
[
set turtle-profiles-habitat this-profile
read
]
]
]
)
end
to read
ask turtle who [
set turtle-profiles-habitat-code reduce_list turtle-profiles-habitat
print ( word "turtle-profiles-habitat-code is: " turtle-profiles-habitat-code )
]
print ( word "turtle-profiles-habitat-code: " turtle-profiles-habitat-code )
print ( word "ValidHabs is: " ValidHabs )
end
to-report reduce_list [a_list]
report reduce word a_list
end
但是它给出了以下错误:
我提出了您的建议,但出现了以下错误:ITEM 预期输入的是数字,但得到的却是字符串“12”。有什么办法可以调整吗?谢谢
这看起来是一个巨大的解决方法,但它确实有效:
report reduce word a_list
仅报告一个字符串,如果 a_list
是一个至少包含 to 条目的列表。如果是例如[1],它只会报告一个数字(我以前不知道这个)
这就是我确保使用 word reduce word a_list ""
创建字符串的原因
然后将该字符串报告为带有 read-from-string
的数字
to-report reduce_list [a_list]
report read-from-string word reduce word a_list ""
end
我有一个列表,我想删除空格和方括号并将其转换为数字以在代码中的其他地方使用。
turtle-profiles-habitat[ [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] ]...
turtle-profiles-habitat-code [ 1 2 3 4 5 12 13 14 15 23 ]...
但是,出现以下错误:ITEM 预期输入的是数字,但得到的却是字符串“12”。
我可以进行字符串到数字的转换吗?
提前致谢
下面的代码
globals [ ValidHabs ]
turtles-own [ turtle-profiles-habitat-code turtle-profiles-habitat t-p-h-item-ValidHabs my-patches ]
patches-own [ habitatcover ]
to setup
ca
set ValidHabs [[1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 3 4 5] [1 2 4 5] [1 2 3 5] [2 3 4 5] [1 2 3 4 5]]
(
foreach ValidHabs [
this-profile ->
ask one-of patches [
sprout 1
[
set turtle-profiles-habitat this-profile
read
]
]
]
)
end
to read
ask turtle who [
set turtle-profiles-habitat-code reduce_list turtle-profiles-habitat
print ( word "turtle-profiles-habitat-code is: " turtle-profiles-habitat-code )
]
print ( word "turtle-profiles-habitat-code: " turtle-profiles-habitat-code )
print ( word "ValidHabs is: " ValidHabs )
end
to-report reduce_list [a_list]
report reduce word a_list
end
但是它给出了以下错误:
我提出了您的建议,但出现了以下错误:ITEM 预期输入的是数字,但得到的却是字符串“12”。有什么办法可以调整吗?谢谢
这看起来是一个巨大的解决方法,但它确实有效:
report reduce word a_list
仅报告一个字符串,如果 a_list
是一个至少包含 to 条目的列表。如果是例如[1],它只会报告一个数字(我以前不知道这个)
这就是我确保使用 word reduce word a_list ""
然后将该字符串报告为带有 read-from-string
to-report reduce_list [a_list]
report read-from-string word reduce word a_list ""
end