← Back Peter

Practice 3: Math Treasure

Name: ZoeChest: ['ruby','diamond','sapphire']

Why?

Use two functions together and peek into a list.

⭐ Stars: 0
🐞 Bugs: 0

Back to Home →



    
Hint
  • print("Hello, " + name.upper() + "!")
  • return a + b
  • print(chest[0], chest[1])
from js import document, localStorage, window import random, re, sys, io START = '''def greet(name): # print Hello, NAME! (uppercase) pass def add(a, b): # return the sum pass def do_math_treasure(name, chest): # 1) call greet(name) # 2) print add(2, 3) # 3) print chest[0], chest[1] pass ''' SOLN = '''def greet(name): print("Hello, " + name.upper() + "!") def add(a, b): return a + b def do_math_treasure(name, chest): greet(name) print(add(2, 3)) print(chest[0], chest[1]) ''' names = ["Kai","Ava","Milo","Zoe","Lia","Noah","Ivy","Owen","Maya","Leo"] NAME = random.choice(names) document.getElementById('nm').textContent = NAME def gi(k): v = localStorage.getItem(k) try: return int(v) if v is not None else 0 except: return 0 def setb(stars=None, bugs=None): if stars is not None: localStorage.setItem('stars', str(stars)) if bugs is not None: localStorage.setItem('bugs', str(bugs)) document.getElementById('stars').textContent = f'⭐ Stars: {gi("stars")}' document.getElementById('bugs').textContent = f'🐞 Bugs: {gi("bugs")}' def run(evt): ns = {}; src = document.getElementById('editor').value; out_el = document.getElementById('out') f = io.StringIO() try: exec(src, ns) for fn in ('greet','add','do_math_treasure'): assert fn in ns, f"Make the function {fn}(...)." chest = ['ruby','diamond','sapphire'] old = sys.stdout; sys.stdout = f try: ns['do_math_treasure'](NAME, chest) finally: sys.stdout = old output = f.getvalue(); up = output.upper() assert ("HELLO " + NAME.upper() + "!") in up, "Say 'Hello, NAME!' (uppercase)." assert "5" in output, "Should print the result of add(2, 3) which is 5." assert "ruby" in output and "diamond" in output, "Print the first two gems." out_el.textContent = "All checks passed! ⭐\n" + output setb(stars=gi('stars')+1) try: window.confetti() except: pass except Exception as e: out_el.textContent = "Bug found: " + str(e) setb(bugs=gi('bugs')+1) document.getElementById('run').addEventListener('click', run) document.getElementById('fill-start').addEventListener('click', lambda e: setattr(document.getElementById('editor'), 'value', START)) document.getElementById('fill-solution').addEventListener('click', lambda e: setattr(document.getElementById('editor'), 'value', SOLN)) if not document.getElementById('editor').value: document.getElementById('editor').value = START setb()