From 34dbc2c3211b5eb1b52e86336e3d78b5fe4b83c7 Mon Sep 17 00:00:00 2001 From: Felienne Date: Thu, 7 May 2020 13:26:02 +0200 Subject: [PATCH] small fix to processing random in level 7 --- hedy.py | 5 ++--- tests.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/hedy.py b/hedy.py index 488ba04ccf6..d7e2cf256bb 100644 --- a/hedy.py +++ b/hedy.py @@ -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] + ']' @@ -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 + "'" @@ -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): diff --git a/tests.py b/tests.py index 48b291b6506..277f15bb93b 100644 --- a/tests.py +++ b/tests.py @@ -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!'