Micro:bit - 加速度计 - (Micro python) 使 microbit 向下运动计数 - 仅适用于手势

Micro:bit - Accelerometer - (Micro python) make microbit count downward movements - only works with gestures

以下代码工作正常:

# Add your Python code here. E.g.
from microbit import *

score = 0
display.show(str(score))

   while True:
    if accelerometer.was_gesture('face down'):
        score += 1
        if score < 10:
            display.show(score)
        else: 
            display.scroll(score)
    continue

'''但是当我尝试将 was_gesture('face down') 替换为 get_Z 时,出现错误:'''

# Add your Python code here. E.g.

    from microbit import *

    score = 0
    display.show(str(score))

    z = accelerometer.get_z()

    while True:
        if z < accelerometer.get_z(-500) 
            score += 1
            if score < 10:
                display.show(score)
            else: 
                display.scroll(score)
        continue

我收到错误消息?但为什么?我只想让微位在每次将设备移动到特定点以下时计数?

您在该行末尾漏掉了一个冒号:

       if z < accelerometer.get_z(-500) 

此外,get_z() 方法不接受任何参数: https://microbit-micropython.readthedocs.io/en/latest/accelerometer.html#microbit.accelerometer.get_z

accelerometer.get_z() 语句需要在 while 循环内,以便更新。该循环还需要一个睡眠语句,这样就不会显示积压的检测结果。

我使用 mu 编辑器在 micro:bit 上测试了下面的代码。当 microbit 的 LED 面朝上时,计数递增。当面朝下时,计数停止。

from microbit import *
uart.init(baudrate=115200)

score = 0
display.show(str(score))

while True:
    z = accelerometer.get_z()
    if z < -500:
        score += 1
        if score < 10:
            display.show(score)
        else: 
            display.scroll(score)
    sleep(1000)
    continue