Netlogo web扩展 “expected a literal value”

huangapple go评论78阅读模式
英文:

Netlogo web extension "expected a literal value"

问题

我有一个模型旨在使用Web扩展与API交互因此在每次移动后我会将我的当前位置和朝向发送到API

这是我使用的代码

```python
to follow-path
  let choice web:make-request "http://127.0.0.1:8000/predict_get" "GET" [] []
  (ifelse
    choice = 0 [
      forward 1
    ]
    choice = 1 [
      right 90
    ]
    choice = 2 [
      left 90
    ]
    ; elsecommands
    [
      stop
  ])
  let data web:make-request "http://127.0.0.1:8000/netlogo" "POST" [["data" (list xcor ycor heading)]] []
end

to move-agent
  ask turtle (count turtles - 1) [follow-path]
end

然后当我检查时,它说在列表部分需要一个“文字值”。如何正确地将数据列表发送到API?

英文:

I have a model which is meant to interact with an api using web extension. So after each move, I will send my current location, heading to the API.

This is the code I am using:

to follow-path
  let choice web:make-request "http://127.0.0.1:8000/predict_get" "GET" [] []
  (ifelse
    choice = 0 [
      forward 1
    ]
    choice = 1 [
      right 90
    ]
    choice = 2 [
      left 90
    ]
    ; elsecommands
    [
      stop
  ])
  let data web:make-request "http://127.0.0.1:8000/netlogo" "POST" [["data" (list xcor ycor heading)]] []
end

to move-agent
  ask turtle (count turtles - 1) [follow-path]
end

Then when I checked, it said "Expected a literal value" at the list part. How can I post the list of data to the api correctly ?

答案1

得分: 1

我认为你在NetLogo中对[[ ]]符号表示法感到困惑。这个表示法允许创建只包含实际值的列表,但无法与变量一起使用。

我建议你尝试在命令中心中使用以下解释以进行示范。

只使用值创建列表:

以下代码:show [ [ "name" "john"] ["surname" "doe"] ]

会打印出以下结果:[["name" "john"] ["surname" "doe"]]

这样,你可以创建只包含字面值的列表。然而,你无法在字面值的地方使用变量。例如,以下代码会导致与你在代码中看到的相同错误:

使用变量创建列表

你应该以嵌套的方式使用list原语来创建web:make-request命令的参数,以解决你的问题。

以下是一个示例用法:

这将打印:

英文:

I think you are struggling with the [[ ]] notation in NetLogo. This notation allows creating lists with only actual values but doesn't work with variables.

I recommend you to test the following explanation in the Command Center for an illustration.

Creating lists with just values:

The following code: show [ [ "name" "john"] ["surname" "doe"] ]

Would print the following outcome: [["name" "john"] ["surname" "doe"]]

This way, you can create lists with literal values. However, you cannot use variables instead of literal values. For example, the following code would give the same error you are seeing in your code:

let name "john"
let surname "doe"
show [ [ "name" name] ["surname" surname] ]

Creating lists with variables

You should use the list primitive in a nested manner to create your arguments for the web:make-request command to solve your problam.

Here's an example usage:

let name "john"
let surname "doe"
show (list (list "name" name) (list "surname" surname))

which would print:

[["name" "john"] ["surname" "doe"]]

huangapple
  • 本文由 发表于 2023年2月16日 07:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定