microbit-foundation / python-editor-v3

Micro:bit Educational Foundation Python Editor V3
https://python.microbit.org
MIT License
57 stars 36 forks source link

Unhelpful error message #1046

Open DaveAtKitronik opened 1 year ago

DaveAtKitronik commented 1 year ago

Consider the following real world example from a novice:

# Imports go at the top
from microbit import *

LED_Pin = pin0

#at the start of each cycle we turn off all the LEDs, so make a function for it.
def LED_AllOff():

    LED_Pin.write_digital(0)

#indicate we are running by turning on the LED
LED_Pin = True

sleep(500)

while True:
    LED_AllOff()
   #todo add some sensing and display here
    sleep(1000)

To make the code readable the novice has defined LED_Pin as a nice alias for Pin 0 (there is an LED connected to it) They are constructing a piece of code that will have multiple LEDs indicating 'stuff', so have defined a function to turn off all of them.

Then they forget they have to call 'write_digital' to access the pin and try to set the pin to True - to turn it on. Python accepts this as a fine and beautiful line of code....

In the while loop they plan to turn off all the LEDs and then sense the temperature, before turning on the appropriate one.

They get a not helpful error message on the line:

LED_Pin.write_digital(0)

image

which is caused by their slip on a later line and python dynamically retyping the variable LED_Pin as a bool.

This is a 'feature' of python, fair enough, but it is a hard error to spot and the editor hasnt helped. (I have trimmed the code to just the minimum to illustrate - there was a lot more which made it more hidden).

Possibly indicating a variable has changed type in some way might help.