CODING
ADVENTURE
0 / 12 completed

CASE 12 / CHAPTER 12

Save code as a contribution

Structured data and text

IN THE STORY

The friends want to collect examples for Code Chronicles without automatically running somebody else's program.

The idea

JSON is a text format for structured data. json.dumps turns a dictionary into JSON text; json.loads turns it back into data. The code field below contains a string, not instructions being run. The example stays in memory. Saving to disk can be added later when file handling has been taught.

Your mission

Convert entry to JSON text, then read it back and display the author and code fields.

Replace ____ with the missing expression, then choose Run code. Match the expected output before trying your own changes.

Before you run it

Will the program print Hello on its own, or display the characters print("Hello")?

A little help?

Hint 1: a nudge

The conversion helper belongs to the imported json module.

Hint 2: a closer look

Use json.dumps(entry).

How the answer works

The code is displayed as text. No eval, exec or import of the contribution is used. A public sharing platform would need additional design; this example is only a local record.

This replaces the code in the editor. Save your version first if you want to keep it.

Make it yours

Add a title field. Try quotes and multiple lines in the code string and confirm the round trip preserves them.

code_chronicles.pyPYTHON 3

Four spaces indent a block. Ctrl / ⌘ + Enter runs your code. Tab moves to the next control.

OUTPUTPython loads when you run
Your program's output will appear here.

Take a guess. Try a line. See what happens.

Expected output

Christine
print("Hello")

Code runs on your device. The first run downloads the Python engine (about 10 MB); later runs use the browser cache. Stop ends a run without losing your code.