英文:
How do I avoid the error: "Expected a literal value"?
问题
我想创建一个情景,在这个情景中,某个事件发生的概率每次事件发生时都会增加。我想为此定义一个局部变量,但出现了错误:
"预期的文字值" 出现(***)
现在我不知道该如何继续了。
代码:
; 如果鸟和猫共享一个区块,猫应该以一定的概率(狩猎技能)捕捉(杀死)鸟
; 这个概率应该在猫成功捕捉一只鸟时增加
to catch-event
ask cats [
let hunting-improvement [ hunting-skill * catch-rate-increase ]
if any? birds-here [
if random-float 1 <= hunting-skill [
ask one-of birds-here [
die
set hunting-skill hunting-skill + hunting-improvement
]
]
]
]
end
英文:
I want to create a scenario where the probability for a certain event to happen increases every time the event happened. I wanted to define a local variable for this but the error:
"Expected literal value" appeared (\*\*\*)
and now I don't know how to continue.
Code:
; If bird and cat share a patch, the cat should catch (kill) the bird with a certain probability (hunting-skill)
; This probablity shoud increase everytime the cat successfully catches a bird
to catch-event
ask cats [
let hunting-improvment [ hunting-skill * catch-rate-increase ]
if any? birds-here [
if random-float 1 <= hunting-skill [
ask one-of birds-here [
die
set hunting-skill hunting-skill + hunting-improvment
]
]
]
]
end
答案1
得分: 1
看起来你在方括号中不正确地使用了 let
。要么完全不使用方括号,要么如果你想使用方括号,就使用圆括号。
以下是你代码的修正最小示例:
breed [cats cat]
breed [birds bird]
cats-own [hunting-skill catch-rate-increase]
to go
create-cats 5 [
set hunting-skill 0
set catch-rate-increase 1
]
end
to catch-event
ask cats [
let hunting-improvement (hunting-skill * catch-rate-increase)
if any? birds-here [
if random-float 1 <= hunting-skill [
ask one-of birds-here [
die
set hunting-skill hunting-skill + hunting-improvement
]
]
]
]
end
希望这对你有所帮助。
英文:
It seems you are using let
incorrectly with square brackets. Either use no brackets at all or if you want to use brackets, use round ones.
Here is a fixed minimum example of your code:
breed [cats cat]
breed [birds bird]
cats-own [hunting-skill catch-rate-increase]
to go
create-cats 5 [
set hunting-skill 0
set catch-rate-increase 1
]
end
to catch-event
ask cats [
let hunting-improvment (hunting-skill * catch-rate-increase)
if any? birds-here [
if random-float 1 <= hunting-skill [
ask one-of birds-here [
die
set hunting-skill hunting-skill + hunting-improvment
]
]
]
]
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论