Notation Issue
In Combinatorial Game Theory, integers like $4$ each denote a particular game or game value, like $\{3\mid\,\}$ (note that we don't write $\varnothing$ in this notation).
As nombre pointed out in the comments, the written equations like "$n=\{\frac{n}{2}\mid\varnothing\}$" are rarely/never true under standard notation for combinatorial games.
If you don't intend to be referencing the standard meanings of $n$, $3n+1$, and $\frac{n}{2}$ in this notation, you should either have a gigantic disclaimer that the usual notation does not apply, or just use something else for the games you would like to describe.
I'll use $g(n)$ where you have $n$, etc. So we have $g(1)=\{\,\mid\,\}$, $g(n)=\{g(n/2)\mid\,\}$ for even $n$, and $g(n)=\{\,\mid g(3n+1)\}$ for odd $n>1$. Technically, this is only a valid definition for all $n$ if the Collatz conjecture is true.
Example Numeric Values
Let's start building a table and see if we see any patterns. $g(1)=\{\,\mid\,\}=0$. $g(2)=\{g(1)\mid\,\}=\{0\mid\,\}=1$. $$\begin{align}g(3)&=\{\,\mid g(10)\}\\&=\{\,\mid \{g(5)\mid\,\}\}\\&=\{\,\mid \{\{\,\mid g(16)\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{g(8)\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{g(4)\mid\,\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\{g(2)\mid\,\}\mid\,\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\{1\mid\,\}\mid\,\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{2\mid\,\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{3\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid 4\}\mid\,\}\}\\&=\{\,\mid \{0\mid\,\}\}\\&=\{\,\mid 1\}\\&=0\end{align}$$ $g(4)=2$, $g(5)=0$, $g(6)=\{g(3)\mid\,\}=\{0\mid\,\}=1$.
$$\begin{align}g(7)&=\{\,\mid g(22)\}\\&=\{\,\mid \{g(11)\mid\,\}\}\\&=\{\,\mid \{\{\,\mid g(34)\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{g(17)\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid g(52)\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{g(26)\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{g(13)\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid g(40)\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid \{g(20)\mid\,\}\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid \{\{g(10)\mid\,\}\mid\,\}\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid \{\{1\mid\,\}\mid\,\}\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid \{2\mid\,\}\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{\{\,\mid 3\}\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{\{0\mid\,\}\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid \{1\mid\,\}\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{\{\,\mid 2\}\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid \{0\mid\,\}\}\mid\,\}\}\\&=\{\,\mid \{\{\,\mid 1\}\mid\,\}\}\\&=\{\,\mid \{0\mid\,\}\}\\&=\{\,\mid 1\}\\&=0\end{align}$$
Claim
In general, the numeric value of $g(n)$ appears to be the highest exponent $m$ such that $2^m$ divides $n$.
Proof
Let's assume, for induction, that the claim is true of all the values of $g$ that arise during the calculation of $g(n)$. Note that $g(1)=0$. If $n$ is even, then $g(n)=\{g(n/2)\mid\,\}=g(n/2)+1$, which matches the highest power of $2$ for $n$. If $n$ is odd and greater than $1$, then $g(n)=\{\,\mid g(3n+1)\}$. Since $3n+1$ is even, $g(3n+1)\ge1$, so that $g(n)=0$, as desired.
Best Answer
Setup
I have a characterization, but the answer is pretty complicated. These definitions depend on the Collatz conjecture being true.
I will define a function related to the Collatz conjecture that associates strings of "$\mathrm{E}$"s and "$\mathrm{O}$"s to the positive integers. I will use multiplicative notation for concatenating strings, so that $\mathrm{E}\left(\mathrm{OE}\right)=\mathrm{EOE}$ and $\mathrm{E}^{3}=\mathrm{EEE}$. Define $C\left(1\right)$ to be the empty string. Then, for $n>1$, define $C(n)=\begin{cases} \mathrm{E}C\left(\frac{n}{2}\right) & \text{if }n\text{ is even}\\ \mathrm{O}C\left(\frac{3n+1}{2}\right) & \text{if }n\text{ is odd} \end{cases}$. For example, $C\left(7\right)=\mathrm{OOOEOEEOEEE}$, with the tail $\mathrm{OEEE}$ coming from $5\overset{\mathrm{O}}{\to}8\overset{\mathrm{E}}{\to}4\overset{\mathrm{E}}{\to}2\overset{\mathrm{E}}{\to}1)$. We can abbreviate this as $C\left(7\right)=\mathrm{O}^{3}\mathrm{E}^{1}\mathrm{O}^{1}\mathrm{E}^{2}\mathrm{O}^{1}\mathrm{E}^{3}$.
Answer
We can express the values of $g$ in terms of $C$ in a relatively simple way. Note that $g\left(1\right)=0$ by definition. For $n>1$, we have the following piecewise formula, where $m\ge1$ is the length of the initial run and $k\ge0$:
$$ g\left(n\right)=\begin{cases} \begin{cases} m-1 & \text{if }C\left(n\right)=\mathrm{E}^{m}\left(\mathrm{OE}\right)^{k}\mathrm{O}^{2}\cdots\\ m & \text{if }C\left(n\right)=\mathrm{E}^{m}\cdots\text{ otherwise} \end{cases} & \text{if }n\text{ is even}\\ \begin{cases} 1-m & \text{if }C\left(n\right)=\mathrm{O}^{m}\left(\mathrm{EO}\right)^{k}\mathrm{E}^{2}\cdots\\ -m & \text{if }C\left(n\right)=\mathrm{O}^{m}\cdots\text{ otherwise} \end{cases} & \text{if }n\text{ is odd} \end{cases} $$ In other words, using $\mathrm{A}$ and $\mathrm{B}$ as stand-ins for $\mathrm{E}$ and $\mathrm{O}$ in some order, and $\left[\mathrm{A}=\mathrm{O}\right]=\begin{cases} 0 & \text{if }\mathrm{A}=\mathrm{E}\\ 1 & \text{if }\mathrm{A}=\mathrm{O} \end{cases}$, we have: $$ g\left(n\right)=\left(-1\right)^{\left[\mathrm{A}=\mathrm{O}\right]}*\begin{cases} m-1 & \text{if }C\left(n\right)=\mathrm{A}^{m}\left(\mathrm{BA}\right)^{k}\mathrm{B}^{2}\cdots\\ m & \text{otherwise} \end{cases} $$
Proof
Since $C\left(n\right)$ determines $n$, I will define $G:\text{strings}\to\mathbb{N}$ so that $g\left(n\right)=G\left(c\right)$ if $c=C\left(n\right)$. In order to show that the above formula for $g$/$G$ is correct for $n>1$, we can induct on the number of alternations between $\mathrm{O}$ and $\mathrm{E}$.
Base cases
Firstly, $G\left(\mathrm{E}^{m}\right)=m$ (including if $m=0$) by a straightforward induction on $m$. And secondly, for $j\ge1$, $$G\left(\mathrm{O}^{j}\mathrm{E}^{m}\right)=\left\{ \mid G\left(\mathrm{O}^{j-1}\mathrm{E}^{m}\right)\right\} =\begin{cases} G\left(\mathrm{O}^{j-1}\mathrm{E}^{m}\right)-1 & \text{if }G\left(\mathrm{O}^{j-1}\mathrm{E}^{m}\right)\le0\\ 0 & \text{if }G\left(\mathrm{O}^{j-1}\mathrm{E}^{m}\right)>0 \end{cases}\text{.}$$ Note that we must have $m\ge3$ (as $1,2,4$ can't be reached from an odd number $n>1$). Therefore, $G\left(\mathrm{E}^{m}\right)>0$ and $G\left(\mathrm{O}\mathrm{E}^{m}\right)=0$. By induction on $j$, $G\left(\mathrm{O}^{j}\mathrm{E}^{m}\right)=1-j$. This agrees with the formula since $m\ge3$, so this has the form $G\left(\mathrm{O}^{j}\left(\mathrm{EO}\right)^{0}\mathrm{E}^{2}\cdots\right)$.
Induction step
E
Assume, for induction purposes, that the formula for $G$ holds for all strings with a given number of alternations that begin with $\mathrm{E}$, so that $G\left(\mathrm{E}^{\ell}\cdots\right)=\begin{cases} \ell-1 & \text{if }\mathrm{E}^{\ell}\left(\mathrm{OE}\right)^{k}\mathrm{O}^{2}\cdots\\ \ell & \text{otherwise} \end{cases}$.
Then we have $$G\left(\mathrm{O}\mathrm{E}^{\ell}\cdots\right)=\begin{cases} \begin{cases} -1 & \text{if }\mathrm{O}\mathrm{E}^{\ell}\left(\mathrm{OE}\right)^{k}\mathrm{O}^{2}\cdots\\ 0 & \text{otherwise} \end{cases} & \text{if }\ell=1\\ 0 & \text{if }\ell>1 \end{cases}=\begin{cases} -1 & \text{if }\left(\mathrm{OE}\right)^{k+1}\mathrm{O}^{2}\cdots\\ 0 & \text{otherwise} \end{cases}\text{.}$$ And $G\left(\mathrm{E}\mathrm{O}\mathrm{E}^{\ell}\cdots\right)=\begin{cases} 0 & \text{if }\mathrm{E}\left(\mathrm{OE}\right)^{k+1}\mathrm{O}^{2}\cdots\\ 1 & \text{otherwise} \end{cases}$. Since those values are nonnegative, we can conclude that $G\left(\mathrm{E}^{m}\mathrm{O}\mathrm{E}^{\ell}\cdots\right)=\begin{cases} m-1 & \text{if }\mathrm{E}^{m}\left(\mathrm{OE}\right)^{k+1}\mathrm{O}^{2}\cdots\\ m & \text{otherwise} \end{cases}$.
For higher powers of $\mathrm{O}$, now consider $G\left(\mathrm{O}^{2}\mathrm{E}^{\ell}\cdots\right)=\begin{cases} -2 & \text{if }\mathrm{O}\left(\mathrm{OE}\right)^{k+1}\mathrm{O}^{2}\cdots\\ -1 & \text{otherwise} \end{cases}$. And so, for $j\ge2$, $G\left(\mathrm{O}^{j}\mathrm{E}^{\ell}\cdots\right)=\begin{cases} -j & \text{if }\mathrm{O}^{j-1}\left(\mathrm{OE}\right)^{k+1}\mathrm{O}^{2}\cdots\\ 1-j & \text{otherwise} \end{cases}$. Since these are all negative, $G\left(\mathrm{E}\mathrm{O}^{j}\mathrm{E}^{\ell}\cdots\right)=0$, and $G\left(\mathrm{E}^{m}\mathrm{O}^{j}\mathrm{E}^{\ell}\cdots\right)=m-1$. Note that $\mathrm{E}^{m}\mathrm{O}^{j}\mathrm{E}^{\ell}\cdots$ has the form $\mathrm{E}^{m}\left(\mathrm{OE}\right)^{0}\mathrm{O}^{2}\cdots$, so this agrees with our formula.
O
The induction steps for adding two alternations that begin with $\mathrm{O}$ are completely analogous. Just flip signs and swap $\mathrm E$ and $\mathrm O$.
Commentary
The formula for $g$ depends on $C$, which basically requires us to run through an unbounded amount of steps of the Collatz sequence for a number, which is not ideal. It also doesn't have an obvious tidy characterization that's not recursive, like "write the $\mathrm{EO}$ string with $1$s and $0$s in a certain way, and then do something with the number to get the value of $G$".
Simple cases
The only thing I could think to do is collect together some simple cases that have a fixed form; but nothing can be done for the general case as that essentially requires you to prove Collatz.
For example, the numbers with $C\left(n\right)=\mathrm{E}^{m}$ are the powers of $2$, with $g\left(2^{m}\right)=m$. These are A000079 in the OEIS.
The numbers of the form $\mathrm{O}\mathrm{E}^{n}$ are those of the form $(4^{k}-1)/3$ for $k\ge1$ (A002450), and they have $g\left(n\right)=0$.
The numbers of the form $\mathrm{O}^{2}\mathrm{E}^{m}$ are those of the form $\left(64^{k}-10\right)/18$ for $k\ge1$ (A228871), and they have $g\left(n\right)=-1$.
The numbers of the form $E^{m}\mathrm{O}\mathrm{E}^{\ell}$ are those of the form $2^{m}(4^{k}-1)/3$ for $k\ge1$ (A181666), and have $g\left(n\right)=m$.
The numbers of the form $E^{m}\mathrm{O^{2}}\mathrm{E}^{\ell}$ are those of the form $2^{m}\left(64^{k}-10\right)/18$ for $k\ge1$ (no OEIS entry), and have $g\left(n\right)=m-1$.
The numbers of the form $\mathrm{O}\mathrm{E}^{m}\mathrm{O}\mathrm{E}^{\ell}$ have one of the two forms $\dfrac{4^{3k+r}-4^{r+1}-6}{18}$ or $\dfrac{4^{3k+r}-4^{r+2}-48}{144}$, etc.
Simpler Code
Your Sage code appears to be doing all of the work of the Conway notation. But since these games only have one move for one of the two players, you can calculate values of $g$ in a much more straightforward way. I imagine you could port the following code to Sage if desired:
Wolfram Language (Mathematica)
Try it online!