Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful error message #1046

Open
DaveAtKitronik opened this issue Oct 21, 2022 · 0 comments
Open

Unhelpful error message #1046

DaveAtKitronik opened this issue Oct 21, 2022 · 0 comments

Comments

@DaveAtKitronik
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant