{"id":631,"date":"2026-11-12T15:21:10","date_gmt":"2026-11-12T19:21:10","guid":{"rendered":"https:\/\/tyler-cooley.com\/?page_id=631"},"modified":"2026-11-17T15:14:29","modified_gmt":"2026-11-17T19:14:29","slug":"coding","status":"publish","type":"page","link":"https:\/\/tyler-cooley.com\/fr\/coding\/","title":{"rendered":"Coding"},"content":{"rendered":"\n<!-- CODING PAGE \u2014 paste into ONE \"Custom HTML\" block on a page called \"Coding\". -->\n<div class=\"tc-site\">\n\n  <header class=\"tc-band tc-band--yellow\">\n    <div class=\"tc-wrap\">\n      <h1><span class=\"tc-ico tc-ico--pixel\" aria-hidden=\"true\"><\/span>Coding<\/h1>\n      <p class=\"tc-hero__lead\">Small Python projects and text games I wrote while learning to code.<\/p>\n      <p>They run right here in your browser. Tap <strong>Start<\/strong>, read the screen, then type your answer and tap <strong>Send<\/strong>. The first one you start takes a few seconds to load.<\/p>\n    <\/div>\n  <\/header>\n\n  <section class=\"tc-band tc-band--blue\">\n    <div class=\"tc-wrap\">\n\n      <!-- Coding projects coming soon. Delete this paragraph when you add your first one. -->\n      <p class=\"tc-hero__lead\">Projects coming soon.<\/p>\n\n      <!-- PROJECT CARD TEMPLATE: to add a project, delete the two lines that say\n           \"DELETE THIS LINE\", fill in the name and description, and paste your\n           Python code where it says, starting at the left edge. -->\n      <!-- DELETE THIS LINE\n      <div class=\"tc-game\" data-tc-game>\n        <h2>[Project name]<\/h2>\n        <p>[One line about it]<\/p>\n<script type=\"text\/x-python\">\n# paste your Python code here\n<\/script>\n      <\/div>\n      DELETE THIS LINE -->\n\n    <\/div>\n  <\/section>\n\n<\/div>\n\n<!-- PYTHON PLAYER \u2014 keep this part once at the bottom of the page. Don't edit. -->\n<script>\n(function () {\n  var PYODIDE = \"https:\/\/cdn.jsdelivr.net\/pyodide\/v0.27.7\/full\/pyodide.js\";\n  var RUNNER = [\n    \"import ast, sys, asyncio, inspect, traceback, textwrap\",\n    \"async def __tc_run(src, ask):\",\n    \"    src = textwrap.dedent(src).strip('\\\\n') + '\\\\n'\",\n    \"    tree = ast.parse(src, '<game>')\",\n    \"    names = {n.name for n in ast.walk(tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and not n.name.startswith('__')}\",\n    \"    class T(ast.NodeTransformer):\",\n    \"        def visit_FunctionDef(self, node):\",\n    \"            self.generic_visit(node)\",\n    \"            if node.name in names:\",\n    \"                return ast.copy_location(ast.AsyncFunctionDef(**{f: getattr(node, f) for f in node._fields}), node)\",\n    \"            return node\",\n    \"        def visit_Call(self, node):\",\n    \"            self.generic_visit(node)\",\n    \"            f = node.func\",\n    \"            name = f.id if isinstance(f, ast.Name) else (f.attr if isinstance(f, ast.Attribute) else None)\",\n    \"            if name in names:\",\n    \"                return ast.copy_location(ast.Await(node), node)\",\n    \"            if name == 'sleep' and (isinstance(f, ast.Name) or (isinstance(f.value, ast.Name) and f.value.id == 'time')):\",\n    \"                call = ast.Call(func=ast.Name('__tc_sleep', ast.Load()), args=node.args, keywords=node.keywords)\",\n    \"                return ast.copy_location(ast.Await(call), node)\",\n    \"            if name == 'input' and isinstance(f, ast.Name):\",\n    \"                return ast.copy_location(ast.Await(node), node)\",\n    \"            return node\",\n    \"    tree = ast.fix_missing_locations(T().visit(tree))\",\n    \"    async def ainput(prompt=''):\",\n    \"        sys.stdout.flush()\",\n    \"        return str(await ask(str(prompt)))\",\n    \"    g = {'__name__': '__main__', 'input': ainput, '__tc_sleep': asyncio.sleep, 'exit': sys.exit, 'quit': sys.exit}\",\n    \"    try:\",\n    \"        code = compile(tree, '<game>', 'exec', flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT)\",\n    \"        res = eval(code, g)\",\n    \"        if inspect.iscoroutine(res):\",\n    \"            await res\",\n    \"    except SystemExit:\",\n    \"        pass\",\n    \"    except Exception as e:\",\n    \"        if '__tc_stop' in str(e):\",\n    \"            return\",\n    \"        traceback.print_exc()\",\n    \"    finally:\",\n    \"        sys.stdout.flush()\",\n    \"        sys.stderr.flush()\"\n  ].join(\"\\n\");\n\n  var pyReady = null;\n  var active = null;\n\n  function loadPy() {\n    if (!pyReady) {\n      pyReady = new Promise(function (resolve, reject) {\n        if (window.loadPyodide) { resolve(); return; }\n        var s = document.createElement(\"script\");\n        s.src = PYODIDE;\n        s.onload = resolve;\n        s.onerror = function () { reject(new Error(\"Could not load Python\")); };\n        document.head.appendChild(s);\n      }).then(function () { return window.loadPyodide(); })\n        .then(function (py) { return py.runPythonAsync(RUNNER).then(function () { return py; }); });\n      pyReady.catch(function () { pyReady = null; });\n    }\n    return pyReady;\n  }\n\n  function setup(game) {\n    if (game.dataset.tcReady) return;\n    game.dataset.tcReady = \"1\";\n    var codeEl = game.querySelector('script[type=\"text\/x-python\"]');\n    var code = codeEl ? codeEl.textContent : \"\";\n    var uid = \"tcg\" + Math.random().toString(36).slice(2, 8);\n\n    var screen = document.createElement(\"div\");\n    screen.className = \"tc-game__screen\";\n    screen.setAttribute(\"role\", \"log\");\n    screen.setAttribute(\"aria-live\", \"polite\");\n    screen.setAttribute(\"aria-label\", \"Game screen\");\n    screen.tabIndex = 0;\n    screen.textContent = \"Tap Start to run it.\";\n\n    var form = document.createElement(\"form\");\n    form.className = \"tc-game__form\";\n    form.hidden = true;\n    form.innerHTML = '<label for=\"' + uid + '\">Type your answer<\/label>' +\n      '<div class=\"tc-game__row\"><input id=\"' + uid + '\" type=\"text\" autocomplete=\"off\" autocapitalize=\"off\" spellcheck=\"false\" enterkeyhint=\"send\">' +\n      '<button type=\"submit\" class=\"tc-btn\">Send<\/button><\/div>';\n    var input = form.querySelector(\"input\");\n\n    var start = document.createElement(\"button\");\n    start.type = \"button\";\n    start.className = \"tc-btn tc-game__start\";\n    start.textContent = \"Start\";\n\n    game.appendChild(screen);\n    game.appendChild(form);\n    game.appendChild(start);\n\n    var pending = null;\n\n    function line(text, cls) {\n      var d = document.createElement(\"div\");\n      if (cls) d.className = cls;\n      d.textContent = text;\n      screen.appendChild(d);\n      screen.scrollTop = screen.scrollHeight;\n    }\n\n    var api = {\n      out: function (t) { line(t); },\n      err: function (t) { line(t, \"tc-game__err\"); },\n      stop: function () {\n        if (pending) { var p = pending; pending = null; form.hidden = true; p.reject(new Error(\"__tc_stop\")); }\n        start.disabled = false; start.textContent = \"Run again\";\n      }\n    };\n\n    function ask(prompt) {\n      if (prompt) line(prompt, \"tc-game__prompt\");\n      form.hidden = false;\n      input.value = \"\";\n      input.focus();\n      return new Promise(function (resolve, reject) { pending = { resolve: resolve, reject: reject }; });\n    }\n\n    form.addEventListener(\"submit\", function (e) {\n      e.preventDefault();\n      if (!pending) return;\n      var v = input.value;\n      line(\"> \" + v, \"tc-game__you\");\n      form.hidden = true;\n      var p = pending; pending = null;\n      p.resolve(v);\n    });\n\n    start.addEventListener(\"click\", function () {\n      if (active && active !== api) active.stop();\n      active = api;\n      start.disabled = true;\n      start.textContent = \"Running\u2026\";\n      screen.textContent = \"\";\n      line(\"Loading\u2026\", \"tc-game__note\");\n      loadPy().then(function (py) {\n        screen.textContent = \"\";\n        py.setStdout({ batched: function (t) { if (active === api) api.out(t); } });\n        py.setStderr({ batched: function (t) { if (active === api) api.err(t); } });\n        py.globals.set(\"__tc_src\", code);\n        py.globals.set(\"__tc_ask\", ask);\n        return py.runPythonAsync(\"await __tc_run(__tc_src, __tc_ask)\");\n      }).then(function () {\n        if (active === api) { line(\"Finished. Tap Run again to restart.\", \"tc-game__note\"); }\n      }).catch(function () {\n        line(\"It couldn't load. Check your internet connection and try again.\", \"tc-game__err\");\n      }).then(function () {\n        form.hidden = true;\n        start.disabled = false;\n        start.textContent = \"Run again\";\n      });\n    });\n  }\n\n  function init() { document.querySelectorAll(\"[data-tc-game]\").forEach(setup); }\n  if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", init); else init();\n})();\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Coding Small Python projects and text games I wrote while learning to code. They run right here in your browser. Tap Start, read the screen, then type your answer and tap Send. The first one you start takes a few seconds to load. Projects coming soon.<\/p>","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"page-with-title","meta":{"footnotes":""},"class_list":["post-631","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/pages\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":1,"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/pages\/631\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/pages\/631\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/tyler-cooley.com\/fr\/wp-json\/wp\/v2\/media?parent=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}