Fix python loop (#4007)

* Fix code generation for python controls_for

* Update python golden file.
This commit is contained in:
Monica Kozbial
2020-06-30 11:35:48 -07:00
committed by GitHub
parent 5c4677010c
commit fcc3a09661
3 changed files with 7 additions and 6 deletions

View File

@@ -156,13 +156,14 @@ Blockly.Python['controls_for'] = function(block) {
if (typeof startVar == 'number' && typeof endVar == 'number') {
if (startVar < endVar) {
range = defineUpRange(startVar, endVar, increment);
range = defineUpRange();
} else {
range = defineDownRange(startVar, endVar, increment);
range = defineDownRange();
}
range += '(' + startVar + ', ' + endVar + ', ' + incVar + ')';
} else {
// We cannot determine direction statically.
range = generateUpDownRange(startVar, endVar, increment);
range = generateUpDownRange(startVar, endVar, incVar);
}
}
code += 'for ' + variable0 + ' in ' + range + ':\n' + branch;

View File

@@ -226,7 +226,7 @@ def test_count_by():
x_start = float(1 + 0)
x_end = float(8 + 0)
x_inc = float(1 - 2)
for x in (x_start <= x_end) and upRange(x_start, x_end, 1 - 2) or downRange(x_start, x_end, 1 - 2):
for x in (x_start <= x_end) and upRange(x_start, x_end, x_inc) or downRange(x_start, x_end, x_inc):
loglist.append(x)
assertEquals(loglist, [1, 2, 3, 4, 5, 6, 7, 8], 'count up non-trivial ints')
loglist = []
@@ -239,7 +239,7 @@ def test_count_by():
x_start3 = float(5 + 0.5)
x_end3 = float(1 + 0)
x_inc2 = float(1 + 0)
for x in (x_start3 <= x_end3) and upRange(x_start3, x_end3, 1 + 0) or downRange(x_start3, x_end3, 1 + 0):
for x in (x_start3 <= x_end3) and upRange(x_start3, x_end3, x_inc2) or downRange(x_start3, x_end3, x_inc2):
loglist.append(x)
assertEquals(loglist, [5.5, 4.5, 3.5, 2.5, 1.5], 'count with floats')

View File

@@ -63,6 +63,6 @@
</block>
<block type="variables_get" x="300" y="100">
<field name="VAR">naked</field>
<comment pinned="true" h="80" w="160">Intentionally non-connected variable.</comment>
<comment pinned="false" h="80" w="160">Intentionally non-connected variable.</comment>
</block>
</xml>