如何在Python中检查值是否不变?

2024-04-28 20:05:48 发布

您现在位置:Python中文网/ 问答频道 /正文

如何检查值是否不变?我的Pi2GO-Lite正在计算它在开车时能走多少步。有时轮子被什么东西卡住了,再也动不了了。遗憾的是,由于缺少传感器,它没有注意到这一点。它有很多盲点。你知道吗

这就是我代码的计数部分:

countL = 0
countR = 0
runL = True
runR = True
running = True
speed = 60


try:

  lastValidL = 2
  lastValidR = 2
  lastL = pi2go.irLeftLine()
  lastR = pi2go.irRightLine()
  while True:
    if pi2go.getDistance() >=8:
      while pi2go.getDistance() >=8:
        if countR > countL:
          pi2go.turnForward(90,85)
        if countR < countL:
          pi2go.turnForward(85,90)
        if countR == countL:
          pi2go.turnForward(85,90)
        val = pi2go.irLeftLine()
        if val == lastL and val != lastValidL:
          countL +=1
          lastValidL = val
        lastL = val
        print "L:", countL
        val = pi2go.irRightLine()
        if val == lastR and val != lastValidR:
          countR += 1
          lastValidR = val
        lastR = val
        print "R:", countR
#heres the function I want.
        oldL = countL +
        print oldL
        if True:
          if oldL == countL:
            time.sleep(5) 
            #if oldL has still the same value as count L
            if oldL == countL 
              pi2go.stepReverse(80,20)

现在我的问题是: “当”时间。睡眠(5)" 柜台也停了。所以countL和oldL肯定和以前一样。你知道吗

  • oldL一直在刷新,所以它的值总是与countL相同

如何使用当前计数的值创建变量,而不使变量随每次计数而更改?如果你知道我的意思,我需要一个“静态”号码。你知道吗


Tags: trueifval计数printcountrcountlpi2go