Repository: hedyorg/hedy

Issue:
Lesson 5: repeat
`repeat` does not take a variable as parameter?

```
n is ask Hoe vaak?
repeat n times print 'Zo vaak!'
```


Discussion:

Resolving patch:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -417,9 +417,9 @@ def number(self, args):
         return ''.join(args)
 
     def repeat(self, args):
-        times = ''.join(args[0])
+        times = wrap_non_var_in_quotes(args[0], self.lookup)
         command = args[1]
-        return f"""for i in range({times}):
+        return f"""for i in range(int({str(times)})):
 {indent(command)}"""
 
 class ConvertToPython_6(ConvertToPython_5):
@@ -491,7 +491,7 @@ def command(self, args):
 
     def repeat(self, args):
         args = [a for a in args if a != ""]  # filter out in|dedent tokens
-        return "for i in range(" +args[0] + "):\n" + "\n".join(args[1:])
+        return "for i in range(int(" + str(args[0]) + ")):\n" + "\n".join(args[1:])
 
     def ifs(self, args):
         args = [a for a in args if a != ""] # filter out in|dedent tokens


Test patch:
diff --git a/tests.py b/tests.py
--- a/tests.py
+++ b/tests.py
@@ -415,7 +415,15 @@ def test_transpile_other(self):
     def test_repeat_basic_print(self):
         result = hedy.transpile("repeat 5 times print 'me wants a cookie!'", 5)
         self.assertEqual(result, """import random
-for i in range(5):
+for i in range(int('5')):
+  print('me wants a cookie!')""")
+        self.assertEqual(run_code(result),'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
+
+    def test_repeat_with_variable_print(self):
+        result = hedy.transpile("n is 5\nrepeat n times print 'me wants a cookie!'", 5)
+        self.assertEqual(result, """import random
+n = '5'
+for i in range(int(n)):
   print('me wants a cookie!')""")
         self.assertEqual(run_code(result),'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
 
@@ -424,13 +432,13 @@ def test_repeat_nested_in_if(self):
         self.assertEqual(result, """import random
 kleur = input('Wat is je lievelingskleur?')
 if kleur == 'groen':
-  for i in range(3):
+  for i in range(int('3')):
     print('mooi!')""")
 
     def test_repeat_over_9_times(self):
         result = hedy.transpile("repeat 10 times print 'me wants a cookie!'", 5)
         self.assertEqual(result, """import random
-for i in range(10):
+for i in range(int('10')):
   print('me wants a cookie!')""")
         self.assertEqual(run_code(result),'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
 
@@ -448,9 +456,18 @@ def test_repeat_nested_in_if(self):
             self.assertEqual(result, """import random
 kleur = input('Wat is je lievelingskleur?')
 if str(kleur) == str('groen'):
-  for i in range(3):
+  for i in range(int('3')):
     print('mooi!')""")
 
+def test_repeat_with_variable_print(self):
+    result = hedy.transpile("n is 5\nrepeat n times print 'me wants a cookie!'", 6)
+    self.assertEqual(result, """import random
+n = '5'
+for i in range(int(n)):
+    print('me wants a cookie!')""")
+    self.assertEqual(run_code(result),
+                     'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
+
     #new tests for calculations
     def test_simple_calculation(self):
         result = hedy.transpile("nummer is 4 + 5", 6)
@@ -534,9 +551,18 @@ def test_repeat_with_indent(self):
         result = hedy.transpile("""repeat 5 times
     print 'koekoek'""", 7)
         self.assertEqual("""import random
-for i in range(5):
+for i in range(int(5)):
     print('koekoek')""",result)
 
+    def test_repeat_with_variable_print(self):
+        result = hedy.transpile("n is 5\nrepeat n times\n    print 'me wants a cookie!'", 7)
+        self.assertEqual(result, """import random
+n = '5'
+for i in range(int(n)):
+    print('me wants a cookie!')""")
+        self.assertEqual(run_code(result),
+                         'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
+
     def test_repeat_nested_in_if(self):
         result = hedy.transpile("""kleur is groen
 if kleur is groen
@@ -545,7 +571,7 @@ def test_repeat_nested_in_if(self):
         self.assertEqual(result, """import random
 kleur = 'groen'
 if str(kleur) == str('groen'):
-    for i in range(3):
+    for i in range(int(3)):
         print('mooi')""")
 
     def test_if_else(self):
@@ -570,7 +596,7 @@ def test_repeat_basic_print(self):
         result = hedy.transpile("""repeat 5 times
     print 'me wants a cookie!'""", 7)
         self.assertEqual(result, """import random
-for i in range(5):
+for i in range(int(5)):
     print('me wants a cookie!')""")
         self.assertEqual(run_code(result),'me wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!\nme wants a cookie!')
 
@@ -580,7 +606,7 @@ def test_repeat_basic_print_multiple_lines(self):
     print 'cookieeee!'
     print 'me wants a cookie!'""", 7)
         self.assertEqual(result, """import random
-for i in range(5):
+for i in range(int(5)):
     print('cookieeee!')
     print('me wants a cookie!')""")
         # self.assertEqual(run_code(result),'cookieeee!\nme wants a cookie!\ncookieeee!\nme wants a cookie!\ncookieeee!\nme wants a cookie!\ncookieeee!\nme wants a cookie!\ncookieeee!\nme wants a cookie!')