#πŸ”’ help fix this code, i am using nested function, this is similar to MST kruskal algorithm

21 messages Β· Page 1 of 1 (latest)

flat pineBOT
#

@barren dust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

barren dust
zinc depot
# barren dust https://paste.pythondiscord.com/ASYQ

What is the output you expect? Currently no one else can run your code, since while you did give the inputs, the pathloss_threshold and curr_platforms variables do not exist. There is also a piece of invalid code, since this parenthesis is never closed.py G = nx.Graph( curr_platforms=[]
Honestly this piece of code looks like the style of ChatGPT.

barren dust
#

sure

#

i used chatgpt but the code doenst look good so i need help

#

@zinc depot I UPDATED THE FILE PLEASE TAKE A LOOK

zinc depot
#

The link is unchanged. You need to post a new link.

barren dust
#

new link

zinc depot
#

#python-discussion message

new link i was able to get chat gpt to produce to correct output but it doenst look clean like mst i need help ty
Are you referring to the code itself, or the output of the code? And what do you mean by "doenst look clean like mst"?

barren dust
#

https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/

basically I am forming a minimum spanning tree but instead of strictly going witht he lowest cost edge I connect edges with higher priority first, this doesnt use nested functions and this does and I have read that nested functions is bad practice and it should never be used

zinc depot
#

Well you'd have to re-write the it so that all the function calls go away, and instead use whatever code is in the function. Also, nested functions are neither bad practice nor "never should be used". They are good for keeping your logic grouped together, and namespace unpolluted. Plus some things basically require them, like decorators. I think you should understand what the code is doing yourself instead of having ChatGPT write it, but in terms of code structure it's fine.

barren dust
#

How did u know that chatgpt wrote it i feel like img oing to get fired now

#

so just one question

zinc depot
#

ChatGPT just has a certain style to the code it writes. Once you've seen enough of it, you can start getting a feel for when code is AI generated.

barren dust
#

But in a situation like this where the output i am trying to do is in a whiel loop that needs to call functions and i encapsulate all that in one big function this situation is okay?

zinc depot
#

Yes. If you don't need the functions elsewhere in your code, there is no point in de-nesting, since all that would do is remove one level on indentation. For something like this, what's the difference between having them all as nested functions, or having them as class methods?

barren dust
#

nothing i just need the output and those functions arent used anywhere else except within that one function

zinc depot
#

Then it's fine. Some parts of the code look like they have very dubious efficiency, like this ```py
for n1 in p1_node:
for n2 in p2_node:
if n1 in selected_nodes or n2 in selected_nodes:
continue

But making those parts faster/use less memory/cleaner will require you to understand the code.
barren dust
#

ok ty for the advice and i will try to optimize this further

flat pineBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.