Skip to content

Unhelpful error message #1046

Open
Open
@DaveAtKitronik

Description

@DaveAtKitronik

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions