英文:
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"]]
这样,你可以创建只包含字面值的列表。然而,你无法在字面值的地方使用变量。例如,以下代码会导致与你在代码中看到的相同错误:
let name "john"
let surname "doe"
show [ [ "name" name] ["surname" surname] ]
使用变量创建列表
你应该以嵌套的方式使用list
原语来创建web:make-request
命令的参数,以解决你的问题。
以下是一个示例用法:
let name "john"
let surname "doe"
show (list (list "name" name) (list "surname" surname))
这将打印:
[["name" "john"] ["surname" "doe"]]
英文:
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"]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论