// Numbas version: finer_feedback_settings {"name": "Use Python code in variable generation", "extensions": ["programming"], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "feedback": {"showactualmarkwhen": "always", "showtotalmarkwhen": "always", "showanswerstatewhen": "always", "showpartfeedbackmessageswhen": "always", "allowrevealanswer": true, "showexpectedanswerswhen": "inreview", "showadvicewhen": "inreview"}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"name": "Use Python code in variable generation", "tags": [], "metadata": {"description": "
Demonstrates how to run Python code to generate the value of a question variable.
", "licence": "Creative Commons Attribution 4.0 International"}, "statement": "We've used Python to generate this random string:
\n{key}", "advice": "", "rulesets": {}, "extensions": ["programming"], "builtin_constants": {"e": true, "pi,\u03c0": true, "i": true, "j": false}, "constants": [], "variables": {"key, hash": {"name": "key, hash", "group": "Ungrouped variables", "definition": "run_code(\"pyodide\", [\"\"\"\nimport hashlib\nimport secrets\nkey = secrets.token_hex()\nm = hashlib.sha256()\nm.update(key.encode('ascii'))\n(key, m.hexdigest())\n\"\"\"]) \n|> then(x -> x[\"code_result\"][0][\"result\"])", "description": "", "templateType": "anything", "can_override": false}, "example_code": {"name": "example_code", "group": "Ungrouped variables", "definition": "safe(\"import hashlib\\nm = hashlib.sha256()\\nm.update(string_to_hash.encode(\\'ascii\\'))\\nhash = m.hexdigest()\\nprint(hash)\")", "description": "", "templateType": "long plain string", "can_override": false}}, "variablesTest": {"condition": "", "maxRuns": 100}, "ungrouped_variables": ["key, hash", "example_code"], "variable_groups": [], "functions": {"wait": {"parameters": [["t", "number"]], "type": "promise", "language": "javascript", "definition": "return new Numbas.jme.types.TPromise(new Promise(resolve => {\n setTimeout(() => resolve(new Numbas.jme.types.TNum(t)), t)\n}));"}}, "preamble": {"js": "", "css": ""}, "parts": [{"type": "patternmatch", "useCustomName": false, "customName": "", "marks": 1, "scripts": {}, "customMarkingAlgorithm": "", "extendBaseMarkingAlgorithm": true, "unitTests": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "variableReplacements": [], "variableReplacementStrategy": "originalfirst", "nextParts": [], "suggestGoingBack": false, "adaptiveMarkingPenalty": 0, "adaptiveMarkingUseCondition": "", "adaptiveMarkingNotUsedMessage": "", "exploreObjective": null, "prompt": "What is the SHA-256 hash of the string, in hexadecimal?
\nTry running this Python code, with the given string:
\n{code_block(example_code, \"python\")}
", "answer": "{hash}", "displayAnswer": "{hash}", "matchMode": "exact", "allowEmpty": false}], "partsMode": "all", "maxMarks": 0, "objectives": [], "penalties": [], "objectiveVisibility": "always", "penaltyVisibility": "always", "showAllParts": false, "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}, {"name": "Christian Lawson-Perfect", "profile_url": "http://clppc.local:8000/accounts/profile/1/"}], "resources": []}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}, {"name": "Christian Lawson-Perfect", "profile_url": "http://clppc.local:8000/accounts/profile/1/"}]}