【问题标题】:Clips programming - comparing the value of global variablesClips编程——比较全局变量的值
【发布时间】:2026-01-21 13:55:02
【问题描述】:

我正在学习 Clips 编程,但遇到了一个问题。我正在尝试使用 if-then-else 语句并测试两个全局变量是否相等。到目前为止,我有以下代码:

(defglobal ?*count_x* = 0)
(defglobal ?*count_y* = 0)
(defrule if_then_else
    ?x <- (?*count_x*)
    ?y <- (?*count_y*)
   =>
   (if  (eq ?x ?y)
      then
      (printout t "Yes, they are equal. " crlf)
      else
      (printout t "No, they are not equal." crlf)
      (printout t "x: " ?*count_x* crlf)
      (printout t "y: " ?*count_y* crlf))
      (retract ?x)
      (retract ?y)
)

这不是所有代码,计数器增加得很好,但这是不能正常工作的部分。有没有办法在不用 defrules 替换全局变量的情况下做到这一点?任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: global-variables clips


    【解决方案1】:

    您不需要在规则的条件中绑定全局变量。直接引用它们:

    (defglobal ?*count_x* = 0)
    (defglobal ?*count_y* = 0)
    (defrule if_then_else
       =>
       (if  (eq ?*count_x* ?*count_y*)
          then
          (printout t "Yes, they are equal. " crlf)
          else
          (printout t "No, they are not equal." crlf)
          (printout t "x: " ?*count_x* crlf)
          (printout t "y: " ?*count_y* crlf))
    )
    

    【讨论】:

    • 谢谢,我修改了,但不知何故它总是打印 Yes 部分(我正在从键盘读取输入并将其从 Python 传递到 Clips 代码)。因此,如果我为 count_x 输入 1,2,3,为 count_y 输入 1,5,6,4,它会打印 Yes,因为第一个值相等。所有计数完成后如何进行测试?尝试显着但没有区别......
    • 没有足够的信息来回答您的问题。
    • 所以我有一个python代码,它接受用户的输入,用户必须一个一个地输入x的一些值,然后在剪辑部分我数y,这是奇数的数量他输入了,我想检查 count_x = count_y,但它总是打印是,即使它不应该......