Repository: hedyorg/hedy

Issue:
allow backwards range in level 8
That would make the fishes song easier, we can later add this as a level (when we do step, too)


Discussion:
Can we have an example? Would we use the Python stepping syntax?
What do you think about:

`for i in range 10 to 1`

I have always found it weird that in Python I have to do range(10,1,-1) while it could know that 10 is more than 1 so I am going backwards.

Might be a bit trickier to build but easy for kids!
That looks good! I believe the same should hold for level 11. `range 10 to 1` should turn into `range(10, 1)`.
> That looks good! I believe the same should hold for level 11. `range 10 to 1` should turn into `range(10, 1)`.

Yes! And then at one point we need to explain to kids (maybe in a fresh level or somewhere in text in a high level) that regular Python does not do this.


Resolving patch:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -844,9 +844,11 @@ def var_access(self, args):
 class ConvertToPython_8(ConvertToPython_7):
     def for_loop(self, args):
         args = [a for a in args if a != ""]  # filter out in|dedent tokens
-        all_lines = [indent(x) for x in args[3:]]
-        return "for " + args[0] + " in range(" + "int(" + args[1] + ")" + ", " + "int(" + args[2] + ")+1" + "):\n"+"\n".join(all_lines)
-
+        body = "\n".join([indent(x) for x in args[3:]])
+        stepvar_name = self.get_fresh_var('step')
+        return f"""{stepvar_name} = 1 if int({args[1]}) < int({args[2]}) else -1
+for {args[0]} in range(int({args[1]}), int({args[2]}) + {stepvar_name}, {stepvar_name}):
+{body}"""
 @hedy_transpiler(level=9)
 @hedy_transpiler(level=10)
 class ConvertToPython_9_10(ConvertToPython_8):


Test patch:
diff --git a/tests/tests_level_08.py b/tests/tests_level_08.py
--- a/tests/tests_level_08.py
+++ b/tests/tests_level_08.py
@@ -129,7 +129,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -163,7 +164,25 @@ def test_forloop(self):
       print i
     print 'wie niet weg is is gezien'""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
+      print(str(i))
+    print('wie niet weg is is gezien')""")
+
+    result = hedy.transpile(code, self.level)
+
+    self.assertEqual(expected, result.code)
+    self.assertEqual(False, result.has_turtle)
+
+
+  def test_reverse_range(self):
+    code = textwrap.dedent("""\
+    for i in range 10 to 1
+      print i
+    print 'wie niet weg is is gezien'""")
+    expected = textwrap.dedent("""\
+    step = 1 if int(10) < int(1) else -1
+    for i in range(int(10), int(1) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -185,7 +204,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
     expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
@@ -210,7 +230,8 @@ def test_for_ifbug(self):
     print 'klaar met for loop'""")
 
     expected = textwrap.dedent("""\
-      for i in range(int(0), int(10)+1):
+      step = 1 if int(0) < int(10) else -1
+      for i in range(int(0), int(10) + step, step):
         antwoord = input('Wat is 5*5')
         if str(antwoord) == str('24'):
           print('fout')
@@ -228,7 +249,8 @@ def test_for_loopbug599(self):
         print '2'""")
 
     expected = textwrap.dedent("""\
-      for i in range(int(0), int(10)+1):
+      step = 1 if int(0) < int(10) else -1
+      for i in range(int(0), int(10) + step, step):
         if str(i) == str('2'):
           print('2')""")
 
diff --git a/tests/tests_level_08_1.py b/tests/tests_level_08_1.py
--- a/tests/tests_level_08_1.py
+++ b/tests/tests_level_08_1.py
@@ -119,7 +119,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -151,7 +152,8 @@ def test_forloop(self):
       print i
     print 'wie niet weg is is gezien'""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
diff --git a/tests/tests_level_09.py b/tests/tests_level_09.py
--- a/tests/tests_level_09.py
+++ b/tests/tests_level_09.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -166,7 +167,8 @@ def test_forloop(self):
       print i
     print 'wie niet weg is is gezien'""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -229,7 +231,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
     expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_10.py b/tests/tests_level_10.py
--- a/tests/tests_level_10.py
+++ b/tests/tests_level_10.py
@@ -131,7 +131,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -140,6 +141,8 @@ def test_for_loop(self):
     self.assertEqual(expected, result.code)
     self.assertEqual(False, result.has_turtle)
 
+
+
   def test_if__else(self):
     code = textwrap.dedent("""\
     a is 5
@@ -165,7 +168,8 @@ def test_forloop(self):
       print i
     print 'wie niet weg is is gezien'""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
     result = hedy.transpile(code, self.level)
@@ -179,8 +183,10 @@ def test_for_nesting(self):
       for j in range 1 to 4:
         print 'rondje: ' i ' tel: ' j""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
     result = hedy.transpile(code, self.level)
@@ -219,7 +225,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
     expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_11.py b/tests/tests_level_11.py
--- a/tests/tests_level_11.py
+++ b/tests/tests_level_11.py
@@ -134,7 +134,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -209,7 +210,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -224,8 +226,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
     result = hedy.transpile(code, self.level)
@@ -261,7 +265,8 @@ def test_newprint(self):
     expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
     result = hedy.transpile(code, self.level)
@@ -281,7 +286,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
     expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_12.py b/tests/tests_level_12.py
--- a/tests/tests_level_12.py
+++ b/tests/tests_level_12.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
     expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -166,7 +167,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -181,8 +183,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
     expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
     result = hedy.transpile(code, self.level)
@@ -218,7 +222,8 @@ def test_newprint(self):
     expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
     result = hedy.transpile(code, self.level)
@@ -297,7 +302,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -320,7 +326,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
     expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_13.py b/tests/tests_level_13.py
--- a/tests/tests_level_13.py
+++ b/tests/tests_level_13.py
@@ -131,7 +131,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -163,7 +164,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -177,8 +179,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -212,7 +216,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -274,7 +279,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -296,7 +302,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_14.py b/tests/tests_level_14.py
--- a/tests/tests_level_14.py
+++ b/tests/tests_level_14.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -164,7 +165,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -178,8 +180,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -213,7 +217,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -275,7 +280,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -297,7 +303,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_15.py b/tests/tests_level_15.py
--- a/tests/tests_level_15.py
+++ b/tests/tests_level_15.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -164,7 +165,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -178,8 +180,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -213,7 +217,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -275,7 +280,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -297,7 +303,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_16.py b/tests/tests_level_16.py
--- a/tests/tests_level_16.py
+++ b/tests/tests_level_16.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -164,7 +165,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -178,8 +180,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -213,7 +217,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -275,7 +280,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -297,7 +303,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_17.py b/tests/tests_level_17.py
--- a/tests/tests_level_17.py
+++ b/tests/tests_level_17.py
@@ -131,7 +131,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -163,7 +164,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -177,8 +179,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -212,7 +216,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -274,7 +279,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -296,7 +302,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_18.py b/tests/tests_level_18.py
--- a/tests/tests_level_18.py
+++ b/tests/tests_level_18.py
@@ -132,7 +132,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -164,7 +165,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -178,8 +180,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -213,7 +217,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -275,7 +280,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -297,7 +303,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
diff --git a/tests/tests_level_19.py b/tests/tests_level_19.py
--- a/tests/tests_level_19.py
+++ b/tests/tests_level_19.py
@@ -129,7 +129,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -161,7 +162,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -175,8 +177,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -210,7 +214,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -272,7 +277,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -294,7 +300,8 @@ def test_if_under_else_in_for(self):
         i is 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
@@ -595,7 +602,8 @@ def test_length2(self):
         print(fruit[i])""")
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
-    for i in range(int(1), int(len(fruit))+1):
+    step = 1 if int(1) < int(len(fruit)) else -1
+    for i in range(int(1), int(len(fruit)) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
@@ -611,7 +619,8 @@ def test_print_length(self):
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
     print('lengte van de lijst is'+str(len(fruit)))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
diff --git a/tests/tests_level_20.py b/tests/tests_level_20.py
--- a/tests/tests_level_20.py
+++ b/tests/tests_level_20.py
@@ -174,7 +174,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -206,7 +207,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -220,8 +222,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -255,7 +259,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -317,7 +322,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -339,7 +345,8 @@ def test_if_under_else_in_for(self):
         i = 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
@@ -640,7 +647,8 @@ def test_length2(self):
         print(fruit[i])""")
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
-    for i in range(int(1), int(len(fruit))+1):
+    step = 1 if int(1) < int(len(fruit)) else -1
+    for i in range(int(1), int(len(fruit)) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
@@ -656,7 +664,8 @@ def test_print_length(self):
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
     print('lengte van de lijst is'+str(len(fruit)))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
diff --git a/tests/tests_level_21.py b/tests/tests_level_21.py
--- a/tests/tests_level_21.py
+++ b/tests/tests_level_21.py
@@ -130,7 +130,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -162,7 +163,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -176,8 +178,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -211,7 +215,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -273,7 +278,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -295,7 +301,8 @@ def test_if_under_else_in_for(self):
         i = 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
@@ -596,7 +603,8 @@ def test_length2(self):
         print(fruit[i])""")
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
-    for i in range(int(1), int(len(fruit))+1):
+    step = 1 if int(1) < int(len(fruit)) else -1
+    for i in range(int(1), int(len(fruit)) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
@@ -612,7 +620,8 @@ def test_print_length(self):
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
     print('lengte van de lijst is'+str(len(fruit)))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
diff --git a/tests/tests_level_22.py b/tests/tests_level_22.py
--- a/tests/tests_level_22.py
+++ b/tests/tests_level_22.py
@@ -130,7 +130,8 @@ def test_for_loop(self):
         expected = textwrap.dedent("""\
     a = '2'
     a = '3'
-    for a in range(int(2), int(4)+1):
+    step = 1 if int(2) < int(4) else -1
+    for a in range(int(2), int(4) + step, step):
       a = int(a) + int(2)
       b = int(b) + int(2)""")
 
@@ -162,7 +163,8 @@ def test_forloop(self):
       print(i)
     print('wie niet weg is is gezien')""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(10)+1):
+    step = 1 if int(1) < int(10) else -1
+    for i in range(int(1), int(10) + step, step):
       print(str(i))
     print('wie niet weg is is gezien')""")
 
@@ -176,8 +178,10 @@ def test_for_nesting(self):
       for j in range(1,4):
         print('rondje: ' i ' tel: ' j)""")
         expected = textwrap.dedent("""\
-    for i in range(int(1), int(3)+1):
-      for j in range(int(1), int(4)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
+      step = 1 if int(1) < int(4) else -1
+      for j in range(int(1), int(4) + step, step):
         print('rondje: '+str(i)+' tel: '+str(j))""")
 
         result = hedy.transpile(code, self.level)
@@ -211,7 +215,8 @@ def test_newprint(self):
         expected = textwrap.dedent("""\
     leeftijd = input('Hoe oud ben jij?')
     print('Dus jij hebt zo veel verjaardagen gehad:')
-    for i in range(int(0), int(leeftijd)+1):
+    step = 1 if int(0) < int(leeftijd) else -1
+    for i in range(int(0), int(leeftijd) + step, step):
       print(str(i))""")
 
         result = hedy.transpile(code, self.level)
@@ -273,7 +278,8 @@ def test_everything_combined(self):
     score = ['100', '300', '500']
     highscore=random.choice(score)
     print('De highscore is: '+str(highscore))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       scorenu=score[i-1]
       print('Score is nu '+str(scorenu))
       if str(highscore) == str('score[i]'):
@@ -295,7 +301,8 @@ def test_if_under_else_in_for(self):
         i = 10""")
 
         expected = textwrap.dedent("""\
-    for i in range(int(0), int(10)+1):
+    step = 1 if int(0) < int(10) else -1
+    for i in range(int(0), int(10) + step, step):
       antwoord = input('Wat is 5*5')
       if str(antwoord) == str('24'):
         print('Dat is fout!')
@@ -596,7 +603,8 @@ def test_length2(self):
         print(fruit[i])""")
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
-    for i in range(int(1), int(len(fruit))+1):
+    step = 1 if int(1) < int(len(fruit)) else -1
+    for i in range(int(1), int(len(fruit)) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)
@@ -612,7 +620,8 @@ def test_print_length(self):
         expected = textwrap.dedent("""\
     fruit = ['appel', 'banaan', 'kers']
     print('lengte van de lijst is'+str(len(fruit)))
-    for i in range(int(1), int(3)+1):
+    step = 1 if int(1) < int(3) else -1
+    for i in range(int(1), int(3) + step, step):
       print(str(fruit[i-1]))""")
 
         result = hedy.transpile(code, self.level)