In this tutorial @Taifun develops a tutorial to run Python code using Skulpt. Thank you @Taifun!
We're going to try it.
I'm going to simplify the Skulpt code and the block code a bit, here they are.
skulpt.htm
<script src="jquery-1.9.0.min.js"></script>
<script src="skulpt.min.js"></script>
<script src="skulpt-stdlib.js"></script>
<script>
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
function outf(text) {
window.AppInventor.setWebViewString("" + text);
}
var prog = window.AppInventor.getWebViewString();
Sk.configure({output:outf, read:builtinRead});
try {
Sk.importMainWithBody("<stdin>",false,prog);
} catch (e) {
alert(e);
}
</script>
pitagoras.py
import math
cateto1 = input("Valor de un cateto: ")
cateto2 = input("Valor del otro cateto: ")
hipotenusa = math.sqrt(math.pow(int(cateto1),2) + math.pow(int(cateto2),2))
print(hipotenusa)
There are still lots of things to implement under the hood, but we have made a huge leap forward in Python 3 compatibility. We will still support Python 2 as an option going forward for projects that rely on it. from https://skulpt.org/
Is it possible to IMPORT eg, a variable or other value, from AI2 into Skulpt/Python, process it there, and then output the result back into AI2 as you show here?
Could you kindly explain the pros and cons of using AI2 blocks vs an extension vs Skulpt/Python. Thanks.
This .py has the same calculation duplicated as result1 & 2 but includes int/float on the second version. Variable n6 is not used in these particular calculations.
CodeSculpt gives the same correct result (198) for both versions:
The problem isn't AI2, it's the way Skulpt adapts the code to Python.
It can be fixed by declaring the variable type.
n1 = float(118)
n2 = float(1)
n3 = float(31)
n4 = float(90)
n5 = float(41)
n6 = float(0)