Skip to content

Commit

Permalink
small fix to processing random in level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Felienne committed May 7, 2020
1 parent 006c039 commit 34dbc2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def assign_list(self, args):
return parameter + " = [" + ", ".join(values) + "]"

def list_access(self, args):
if args[1] == 'random':
if args[1] == '':
return 'random.choice(' + args[0] + ')'
else:
return args[0] + '[' + args[1] + ']'
Expand Down Expand Up @@ -444,7 +444,7 @@ def assign(self, args): #TODO: needs to be merged with 6, when 6 is improved to
if type(value) is Tree:
return parameter + " = " + value.children
else:
if "'" in value:
if "'" in value or 'random.choice' in value: #not amainzg, should still be done with lookup
return parameter + " = " + value
else:
return parameter + " = '" + value + "'"
Expand All @@ -467,7 +467,6 @@ def var_access(self, args):
# dit was list_access
return args[0] + "[" + str(args[1]) + "]" if type(args[1]) is not Tree else "random.choice(" + str(args[0]) + ")"


class ConvertToPython(Transformer):

def start(self, args):
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,16 @@ def test_repeat_basic_print(self):
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_print_random(self):
result = hedy.transpile("""keuzes is steen, schaar, papier
computerkeuze is keuzes at random
print 'computer koos ' computerkeuze""", 7)
self.assertEqual("""import random
keuzes = ['steen', 'schaar', 'papier']
computerkeuze = random.choice(keuzes)
print('computer koos '+str(computerkeuze))""", result)


def test_repeat_basic_print_multiple_lines(self):
result = hedy.transpile("""repeat 5 times
print 'cookieeee!'
Expand Down

0 comments on commit 34dbc2c

Please sign in to comment.