-2

I met a problem in this function,

why is nodes is []? It sometimes is normal, same with total_nodes, but every once in 2 calls it became [].

Why? How to make it as same as total_nodes?

def generate_element(
    x, y, z_top, z_bottom, grayscale, material: Material, nodes: list[Node]
) -> Tuple[Element, list[Node]]:
    print(f"{node_no(nodes, x, y, z_bottom) = }")
    print(f"{nodes = }")
    try:
        input("Press Enter...")
    except KeyboardInterrupt:
        exit(1)

total_nodes = []
for lindex, layer in enumerate(layers):
    element, ns = generate_element(
        x,
        y,
        z_top=zs[lindex + 1],
        z_bottom=zs[lindex],
        grayscale=grayscale,
        material=layer.material_type,
        nodes=total_nodes,
    )
    elements.append(element)
    total_nodes += ns
    print(f"{total_nodes = }")

Expected:

nodes arg is always as same as total_nodes variable.

  • 1
    You're printing `nodes` before you extend the list, and `total_nodes` after. – user2357112 Aug 24 '23 at 03:15
  • Right. That means the `nodes` you print in `generate_element` will match the PREVIOUS printed value of `total_nodes`. – Tim Roberts Aug 24 '23 at 04:00
  • Note: It's not initialization problem. It continues becomes [] every two iterations. Please git clone and execute my code, thanks. – Drunkwcodes Aug 24 '23 at 05:44
  • 1) this is not a minimal working example. 2) your function isn't returning anything (or just `None` to be correct). Please check [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – deponovo Aug 24 '23 at 06:19
  • @deponovo I put my github repo in the post, the link anchor is in the "function" word. – Drunkwcodes Aug 24 '23 at 07:20

0 Answers0