r/learnpython • • 59m ago

Trying to count a specific way.

Hello.
I am currently trying to create an energy level diagram and I need a way to count how many electrons are in each energy level.
For thse who don't know electrons pair up on each energy level.
So a diagram dipicting 3 electrons would look like this:

|
|---- -√2 β
|
|
|--↑-- 0 β
|
|-↑--↓- √2 β

Whilst a diagram depicting 4 electrons would lok like this:

|
|---- -√2 β
|
|
|-↑--↓- 0 β
|
|-↑--↓- √2 β

I currently have a set up where the orizontal lines are hlines that call back to dictionary and I'm currently trying to get the arrows to plot via the ax.annotate function to varying succsess. I can get them on the lines but they still print on every energy level rather than the lowest going upwards.

2 Upvotes

3 comments sorted by

•

u/bytemender_dev 49m ago

I'd work out the counts first, then draw arrows from those — I kept getting arrows on every level until I split it that way. If you put your levels in a list from lowest to highest, you can hand out two electrons at a time:

remaining = n_electrons

for y in levels_bottom_to_top:

count = min(2, remaining)

remaining -= count

if count >= 1:

ax.annotate("↑", xy=(0, y))

if count == 2:

ax.annotate("↓", xy=(0.2, y))

With 3 electrons that gives you counts of 2, 1, 0, so the top level just never gets drawn on.

•

u/Joltik2007 28m ago

Hi, thanks for the quick response. I've tried importing your code into what I currently have and it only generates a single upwards arrow on the lowest energy level.

•

u/ResidentPomegranate6 11m ago

the annotate call is probably inside the loop that walks levels, so every level gets an arrow. build occupancy first, divmod(n, 2) gives level and slot per electron, then draw each occupied slot in a second pass.