在 Skulpt 的 turtle.color() 中传递一个标量有什么影响?
What's the effect of passing a scalar in turtle.color() in Skulpt?
当我尝试 运行 下面的 python 代码时,turtle.color 如何将输入解释为数字?
import turtle
for i in ['red', 'blue', 'green', 'pink', 8, 10]:
turtle.color(i)
turtle.forward(100)
turtle.right(90)
实际:
我得到的输出是一个边长按给定顺序排列的正方形。
当它到达turtle.color(8)
时,其中一侧被黑色覆盖,
接下来是下一面 (turtle.color(10)
).
预计:
代码应该会出错 turtle.color(8)
,没有意义!!
我实际上是在使用在线 turtle 编译器来测试我的代码 (repl.it/languages/python_turtle)。
来自他们的 blog, repl.it mentions they use skulpt 作为他们的 webIDE。
Skulpt 的 Github 页面显示了以下函数,表明 'Black' 是默认值。这解释了您在调试时看到的与其他人相比的奇怪行为。
function createColor(color, g, b, a) {
var i;
if (g !== undefined) {
color = [color, g, b, a];
}
if (color.constructor === Array && color.length) {
for(i = 0; i < 3; i++) {
color[i] = (typeof color[i] === "number") ?
Math.max(0, Math.min(255, parseInt(color[i]))) :
0;
}
if (typeof color[i] === "number") {
color[3] = Math.max(0, Math.min(1, color[i]));
color = "rgba(" + color.join(",") + ")";
}
else {
color = "rgb(" + color.slice(0,3).join(",") + ")";
}
}
else if (typeof color === "string" && !color.match(/\s*url\s*\(/i)) {
color = color.replace(/\s+/g, "");
}
else {
return "black";
}
return color;
}
当我尝试 运行 下面的 python 代码时,turtle.color 如何将输入解释为数字?
import turtle
for i in ['red', 'blue', 'green', 'pink', 8, 10]:
turtle.color(i)
turtle.forward(100)
turtle.right(90)
实际:
我得到的输出是一个边长按给定顺序排列的正方形。
当它到达turtle.color(8)
时,其中一侧被黑色覆盖,
接下来是下一面 (turtle.color(10)
).
预计:
代码应该会出错 turtle.color(8)
,没有意义!!
我实际上是在使用在线 turtle 编译器来测试我的代码 (repl.it/languages/python_turtle)。
来自他们的 blog, repl.it mentions they use skulpt 作为他们的 webIDE。
Skulpt 的 Github 页面显示了以下函数,表明 'Black' 是默认值。这解释了您在调试时看到的与其他人相比的奇怪行为。
function createColor(color, g, b, a) {
var i;
if (g !== undefined) {
color = [color, g, b, a];
}
if (color.constructor === Array && color.length) {
for(i = 0; i < 3; i++) {
color[i] = (typeof color[i] === "number") ?
Math.max(0, Math.min(255, parseInt(color[i]))) :
0;
}
if (typeof color[i] === "number") {
color[3] = Math.max(0, Math.min(1, color[i]));
color = "rgba(" + color.join(",") + ")";
}
else {
color = "rgb(" + color.slice(0,3).join(",") + ")";
}
}
else if (typeof color === "string" && !color.match(/\s*url\s*\(/i)) {
color = color.replace(/\s+/g, "");
}
else {
return "black";
}
return color;
}