如何避免错误:”期望一个字面值”?

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

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:

&quot;Expected literal value&quot; 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 &lt;= 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 &lt;= hunting-skill [
        ask one-of birds-here [
          die 
          set hunting-skill hunting-skill + hunting-improvment
        ]
      ]
    ]
  ]
end

huangapple
  • 本文由 发表于 2023年7月27日 18:17:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778741.html
匿名

发表评论

匿名网友

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

确定