Repository: hedyorg/hedy

Issue:
Add a tiny sleep after turtle.forward
So it is more clear the turtle makes multiple steps when repeating forwards


Discussion:

Resolving patch:
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -289,7 +289,7 @@ def parse():
 
             response['has_turtle'] = has_turtle
             if has_turtle:
-                response["Code"] = "# coding=utf8\nimport random\nimport turtle\nt = turtle.Turtle()\nt.forward(0)\n" + python_code
+                response["Code"] = "# coding=utf8\nimport random\nimport time\nimport turtle\nt = turtle.Turtle()\nt.forward(0)\n" + python_code
             else:
                 response["Code"] = "# coding=utf8\nimport random\n" + python_code
         except hedy.HedyException as E:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -464,7 +464,10 @@ def forward(self,args):
             parameter = int(args[0])
         except:
             parameter = 50
-        return f"t.forward({parameter})"""
+        return self.make_forward(parameter)
+
+    def make_forward(self, parameter):
+        return f"t.forward({parameter})""\ntime.sleep(0.1)"
 
     def turn(self, args):
         if len(args) == 0:
@@ -532,20 +535,19 @@ def print(self, args):
 
     def forward(self, args):
         # no args received? default to 50
-        if len(args) == 0:
-            return "t.forward(50)"
+        parameter = 50
 
-        parameter = args[0]
-        #if the parameter is a variable, print as is
-        if parameter in self.lookup:
-            return f"t.forward({parameter})"
+        if len(args) > 0:
+            parameter = args[0]
 
+        #if the parameter is a variable, print as is
         # otherwise, see if we got a number. if not, simply use 50 as default
         try:
-            parameter = int(args[0])
+            if parameter not in self.lookup:
+                parameter = int(parameter)
         except:
             parameter = 50
-        return f"t.forward({parameter})"""
+        return self.make_forward(parameter)
 
     def ask(self, args):
         var = args[0]


Test patch:
diff --git a/tests/tests_level_01.py b/tests/tests_level_01.py
--- a/tests/tests_level_01.py
+++ b/tests/tests_level_01.py
@@ -82,7 +82,8 @@ def test_one_forward_has_turtle(self):
   def test_transpile_one_forward(self):
     result = hedy.transpile("forward 50", self.level)
     expected = textwrap.dedent("""\
-    t.forward(50)""")
+    t.forward(50)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
 
   def test_transpile_turn_no_args(self):
@@ -117,7 +118,9 @@ def test_transpile_multiple_forward_without_arguments(self):
     result = hedy.transpile("forward\nforward", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
-    t.forward(50)""")
+    time.sleep(0.1)
+    t.forward(50)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -125,8 +128,10 @@ def test_transpile_turtle_combi(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
diff --git a/tests/tests_level_02.py b/tests/tests_level_02.py
--- a/tests/tests_level_02.py
+++ b/tests/tests_level_02.py
@@ -129,8 +129,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -143,7 +145,8 @@ def test_transpile_forward_without_argument(self):
     expected = textwrap.dedent("""\
     hoek = '90'
     t.right(hoek)
-    t.forward(50)""")
+    t.forward(50)
+    time.sleep(0.1)""")
 
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
@@ -155,7 +158,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan'+'?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
diff --git a/tests/tests_level_03.py b/tests/tests_level_03.py
--- a/tests/tests_level_03.py
+++ b/tests/tests_level_03.py
@@ -58,8 +58,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -70,7 +72,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
diff --git a/tests/tests_level_04.py b/tests/tests_level_04.py
--- a/tests/tests_level_04.py
+++ b/tests/tests_level_04.py
@@ -70,8 +70,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -89,7 +91,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
diff --git a/tests/tests_level_05.py b/tests/tests_level_05.py
--- a/tests/tests_level_05.py
+++ b/tests/tests_level_05.py
@@ -61,8 +61,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -73,7 +75,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -111,7 +114,8 @@ def test_repeat_turtle(self):
 
     expected = textwrap.dedent("""\
     for i in range(int('3')):
-      t.forward(100)""")
+      t.forward(100)
+      time.sleep(0.1)""")
 
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
diff --git a/tests/tests_level_06.py b/tests/tests_level_06.py
--- a/tests/tests_level_06.py
+++ b/tests/tests_level_06.py
@@ -129,8 +129,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
@@ -141,7 +143,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
     self.assertEqual(True, result.has_turtle)
 
diff --git a/tests/tests_level_07.py b/tests/tests_level_07.py
--- a/tests/tests_level_07.py
+++ b/tests/tests_level_07.py
@@ -55,8 +55,10 @@ def test_transpile_turtle_basic(self):
     result = hedy.transpile("forward 50\nturn\nforward 100", self.level)
     expected = textwrap.dedent("""\
     t.forward(50)
+    time.sleep(0.1)
     t.right(90)
-    t.forward(100)""")
+    t.forward(100)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
 
   def test_transpile_turtle_with_ask(self):
@@ -66,7 +68,8 @@ def test_transpile_turtle_with_ask(self):
     result = hedy.transpile(code, self.level)
     expected = textwrap.dedent("""\
     afstand = input('hoe ver dan?')
-    t.forward(afstand)""")
+    t.forward(afstand)
+    time.sleep(0.1)""")
     self.assertEqual(expected, result.code)
 
   def test_print_with_calc_no_spaces(self):