Repository: hedyorg/hedy

Issue:
[BUG] Unable to save random value in var in level 3
**Paste the Hedy code & level**
```
dieren is hond, kat, kangoeroe
dier is dieren at random
print dier
```

**Add a screenshot (optional)**
![image](https://user-images.githubusercontent.com/1003685/148047443-6988a0bb-9a2b-46a5-9027-84527324b92f.png)

**Expected behavior**
Should print an element of the list
**What machine and browser you were using (optional)**
If the issue concerns things in the website, let us know:
- What computer you are using (Windows, Mac, Linux?)
- What browser you were using (Chrome, Edge, Safari)



Discussion:

Resolving patch:
diff --git a/hedy.py b/hedy.py
--- a/hedy.py
+++ b/hedy.py
@@ -1164,9 +1164,12 @@ def turn(self, args):
     def assign(self, args):
         parameter = args[0]
         value = args[1]
-        #if the assigned value contains single quotes, escape them
-        value = process_characters_needing_escape(value)
-        return parameter + " = '" + value + "'"
+        if is_random(value):
+            return parameter + " = " + value
+        else:
+            # if the assigned value contains single quotes, escape them
+            value = process_characters_needing_escape(value)
+            return parameter + " = '" + value + "'"
 
 
     def sleep(self, args):
@@ -1261,9 +1264,9 @@ class ConvertToPython_5(ConvertToPython_4):
     def list_access_var(self, args):
         var = hash_var(args[0])
         if args[2].data == 'random':
-            return var + '=random.choice(' + args[1] + ')'
+            return var + ' = random.choice(' + args[1] + ')'
         else:
-            return var + '=' + args[1] + '[' + args[2].children[0] + '-1]'
+            return var + ' = ' + args[1] + '[' + args[2].children[0] + '-1]'
 
     def ifs(self, args):
         return f"""if {args[0]}:


Test patch:
diff --git a/tests/test_level_03.py b/tests/test_level_03.py
--- a/tests/test_level_03.py
+++ b/tests/test_level_03.py
@@ -133,6 +133,25 @@ def test_assign_list(self):
     dieren = ['Hond', 'Kat', 'Kangoeroe']""")
 
     self.single_level_tester(code=code, expected=expected)
+  def test_assign_random_value(self):
+    code = textwrap.dedent("""\
+    dieren is hond, kat, kangoeroe
+    dier is dieren at random
+    print dier""")
+
+    expected = textwrap.dedent("""\
+    dieren = ['hond', 'kat', 'kangoeroe']
+    dier = random.choice(dieren)
+    print(f'{dier}')""")
+
+    list = ['Hond', 'Kat', 'Kangoeroe']
+
+    self.multi_level_tester(
+      code=code,
+      expected=expected,
+      extra_check_function=self.result_in(list),
+      max_level=11)
+
   def test_assign_list_exclamation_mark(self):
     code = textwrap.dedent("""\
     antwoorden is ja, NEE!, misschien
diff --git a/tests/test_level_05.py b/tests/test_level_05.py
--- a/tests/test_level_05.py
+++ b/tests/test_level_05.py
@@ -10,19 +10,7 @@ class TestsLevel5(HedyTester):
   # print & ask -> no changes, covered by tests of earlier levels
 
   # is
-  def test_assign_list_access(self):
-    code = textwrap.dedent("""\
-    dieren is Hond, Kat, Kangoeroe
-    dier is dieren at random
-    print dier""")
-
-    expected = textwrap.dedent("""\
-    dieren = ['Hond', 'Kat', 'Kangoeroe']
-    dier=random.choice(dieren)
-    print(f'{dier}')""")
 
-    list = ['Hond', 'Kat', 'Kangoeroe']
-    self.single_level_tester(code=code, expected=expected, extra_check_function=self.result_in(list))
 
   def test_assign_list_multiple_spaces(self):
     code = textwrap.dedent("""\
@@ -32,7 +20,7 @@ def test_assign_list_multiple_spaces(self):
 
     expected = textwrap.dedent("""\
     dieren = ['Hond', 'Kat', 'Kangoeroe']
-    dier=random.choice(dieren)
+    dier = random.choice(dieren)
     print(f'{dier}')""")
 
     self.single_level_tester(code=code, expected=expected)
@@ -192,7 +180,7 @@ def test_if_else_newline_list_assigment_print(self):
 
     expected = textwrap.dedent("""\
     people = ['mom', 'dad', 'Emma', 'Sophie']
-    dishwasher=random.choice(people)
+    dishwasher = random.choice(people)
     if dishwasher == 'Sophie':
       print(f'too bad I have to do the dishes')
     else:
@@ -240,7 +228,7 @@ def test_print_if_linebreak_statement(self):
 
     expected = textwrap.dedent("""\
     people = ['1', '2', '3', '3']
-    dishwasher=random.choice(people)
+    dishwasher = random.choice(people)
     test = '1'
     if dishwasher == test:
       print(f'too bad I have to do the dishes!')""")
diff --git a/tests/test_level_06.py b/tests/test_level_06.py
--- a/tests/test_level_06.py
+++ b/tests/test_level_06.py
@@ -270,7 +270,7 @@ def test_print_else(self):
       expected = textwrap.dedent("""\
       keuzes = ['1', '2', '3', '4', '5', 'regenworm']
       punten = '0'
-      worp=random.choice(keuzes)
+      worp = random.choice(keuzes)
       if str(worp) == str('regenworm'):
         punten = int(punten) + int(5)
       else:
diff --git a/tests/test_level_12.py b/tests/test_level_12.py
--- a/tests/test_level_12.py
+++ b/tests/test_level_12.py
@@ -176,6 +176,7 @@ def test_ask_with_number_var(self, number):
       expected=expected
     )
 
+  #list tests
   def test_ask_with_list_gives_type_error(self):
     code = textwrap.dedent("""\
       colors is 'orange', 'blue', 'green'
@@ -187,6 +188,25 @@ def test_ask_with_list_gives_type_error(self):
       exception=hedy.exceptions.InvalidArgumentTypeException
     )
 
+  def test_assign_random_value(self):
+    code = textwrap.dedent("""\
+    dieren is 'hond', 'kat', 'kangoeroe'
+    dier is dieren at random
+    print dier""")
+
+    expected = textwrap.dedent("""\
+    dieren = ['hond', 'kat', 'kangoeroe']
+    dier = random.choice(dieren)
+    print(f'{dier}')""")
+
+    list = ['Hond', 'Kat', 'Kangoeroe']
+
+    self.multi_level_tester(
+      code=code,
+      expected=expected,
+      extra_check_function=self.result_in(list),
+      max_level=15)
+
   def test_if_in_list_with_string_var_gives_type_error(self):
     code = textwrap.dedent("""\
     items is 'red'
@@ -489,7 +509,7 @@ def test_list_creation_with_numbers(self):
     getal is getallen at random""")
     expected = textwrap.dedent("""\
     getallen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-    getal=random.choice(getallen)""")
+    getal = random.choice(getallen)""")
 
 
     self.single_level_tester(code=code, expected=expected)