如何解决错误:Item期望输入的是数字,但在NetLogo中却得到了字符串&q;12&q;? [英] How to resolve the error: ITEM expected input to be a number but got the string "12" instead in NetLogo?

查看:10
本文介绍了如何解决错误:Item期望输入的是数字,但在NetLogo中却得到了字符串&q;12&q;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要去掉空格和方括号并转换为数字以便在代码中的其他位置使用的列表。

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 ]...

但是,出现以下错误:项目输入应为数字,但实际输入的字符串为";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期望输入的是数字,但得到的却是字符串&Quot;12&Quot;。有什么办法可以调整这个吗?谢谢

推荐答案

这看起来像是一个巨大的解决办法,但它是有效的:

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

这篇关于如何解决错误:Item期望输入的是数字,但在NetLogo中却得到了字符串&q;12&q;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆