【问题标题】:Raspberry Pi Pico - problem with Thonny interpreterRaspberry Pi Pico - Thonny 解释器的问题
【发布时间】:2021-02-17 13:02:08
【问题描述】:

我在编写 Raspberry Pi Pico 时遇到问题。我正在使用 Thonny IDE 和 micropython。我只是一个初学者,所以只需从他们的网站 (https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/6) 下载代码并将其安装到微控制器。但是当我保存这段代码时:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
    led.toggle()
    time.sleep(0.5)

我收到这条消息:

Traceback(最近一次调用最后一次): 文件“”,第 10 行 IndentationError: unindent 不匹配任何外部缩进级别 你能帮帮我吗?

【问题讨论】:

  • 我想知道你是否混合了空格和制表符。您应该始终使用 4 个空格。见这里 - stackoverflow.com/questions/14979224/…
  • 您的错误信息提到了第 10 行,但上面显示的只有 8 行
  • 我认为你应该在 while 语句后缩进。 PS - Thonny 通常会检测制表符并建议您将它们更改为 4 个空格。

标签: raspberry-pi embedded interpreter micropython thonny


【解决方案1】:

您需要正确缩进代码:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
    if button.value():
        led.toggle()
        time.sleep(0.5)

【讨论】:

    【解决方案2】:

    不妨试试这个:

    from machine import Pin
    import time
    led = Pin(15, Pin.OUT)
    button = Pin(14, Pin.IN, Pin.PULL_DOWN)
    while True:
        if button.value():
            led.toggle()
            time.sleep(0.5)
    

    我刚刚更改了 if button.value(): 四个空格,所以它在 while true: 循环内。

    【讨论】:

    • 错误:if 句后没有缩进,第一个语句有不必要的缩进
    猜你喜欢
    • 2023-01-03
    • 2023-02-17
    • 2023-02-17
    • 2023-01-19
    • 2022-08-14
    • 2022-09-24
    • 2022-09-25
    • 2022-11-22
    • 1970-01-01
    相关资源
    最近更新 更多