A little exchange
Python: "I want your silly game to have a starting value of 100 for life points for all players."
Python: "Let's see...oh wow. Your game code actually worked. Alfred now has 99 life points and Wallace has 101. Good job us."
So, this silly example can be translated into code as follows.
First, things are actually easy for us because we are in a Jupyter notebook living in a browser, utilizing all sorts of widgets and HTML elements already. We can tap into that (and actually the DOM) to get some of the functionality we require.
So, in Python we can use the IPython.display module with the HTML function allowing us to embed an element for later use.
# Python from IPython.display import HTML pystartlife = str(100) HTML("<div id='textid'>" + pystartlife + "</div>")
We've created a Python variable,
pystartlife, and embedded it as a
div element with the Python variable from the document or webpage and play our game. We also write the game. (This game is based on a Node.js tutorial by thenewboston on YouTube - thank you Bucky!).
We grab the Python variable now embedded with
document.getElementById('idname').innerHTML, a DOM function and attribute which grabs the
div by its id (not name) and takes what is in the text part of the
And to see if the magic worked, we run a simple couple of print statements back in Python:
# Python print(Alfred_life) print(Wallace_life)
With low and behold and output of: 99 and 101. Way to pay it forward, Alfred!
You can find this code and more in this Jupyter notebook.