Using normal python in makecode but it's not working

Hello, I am volunteering to help teach Intro to CS with Makecode. We are in unit 3 currently (of semester 1). I am looking ahead at the prospect of switching to python or typescript which I think happens in a later unit or even semester 2. I am testing makecode with some simple python code but finding it doesn’t seem to handle it well. The error it reports doesn’t make sense and it is referencing a javascript/typescript function. It looks to me like typescript may be the language it actually runs in? The code is pretty simple but doesn’t work. It works fine in VSCode or a typescript online repl if I change the game.splash() to print(). This is giving me doubts about using the makecode curriculum after it switches to python from blocks. Does anyone have any comments?

def naive(all_the_numbers, max_num):
    for outer in range(max_num):
        for inner in range(2, outer):
            if outer % inner == 0:
                all_the_numbers[outer] = False
                break

MAX_NUM = 15
all_the_numbers = [True]
for push_index in range(0, MAX_NUM):
    all_the_numbers.append(True)
naive(all_the_numbers, MAX_NUM)
     
for print_index in range(MAX_NUM - 1):
    if all_the_numbers[print_index] == True:
        game.splash(print_index)
2 Likes

Would you mind sharing the link to this project so I can take a look? It will help me understand the errors being suggested so I can get a better idea of what’s going on.

Hi, sorry I didn’t see your email til just now.

(it’s an example of a naive approach to finding prime numbers). I just grabbed it as some sample python to try in MakeCode.

This is a bug in MakeCode, I’ve filed an issue for it here: https://github.com/microsoft/pxt-arcade/issues/6320

As a workaround, you can fix it by adding a type hint to the all_the_numbers parameter like so:

def naive(all_the_numbers: List[bool], max_num):
    for outer in range(max_num):
        for inner in range(2, outer):
            if outer % inner == 0:
                all_the_numbers[outer] = False
                break

MAX_NUM = 15
all_the_numbers = [True]
for push_index in range(0, MAX_NUM):
    all_the_numbers.append(True)
naive(all_the_numbers, MAX_NUM)
     
for print_index in range(MAX_NUM - 1):
    if all_the_numbers[print_index] == True:
        game.splash(print_index)

note the : List[bool] after the all_the_numbers parameter

1 Like

Sorry, but playing around a little more I found something else. This line of code is reported as invalid, but it is also normal python:
all_the_numbers = [True] * MAX_NUM

(this would create an array of True booleans of length MAX_NUM)

MakeCode responds with an error message:
Line 9: types not compatible: boolean and number

I see that that fixes it. It seems like the Python in MakeCode is not vanilla Python, as I think is indicated in the docs. Is there documentation for the differences?

but i’m not sure those docs are complete. makecode python is referred to as “static python”, and it does not support everything in normal python

1 Like

The MakeCode Python documentation is limited. This cheat sheet for type hints in python has been helpful.

# Type hints cheat sheet

1 Like