Python Battle Code Broken

I decided to have some fun with Python Code, and for some reason, it glitches out of all the for loops and skips all the other hero’s moves. It is supposed to run through all the heroes first, then run the enemies’ AI code. However, it keeps on looping through the same hero over and over again, while after the same hero moves, the entire team of enemies moved.

Link: https://replit.com/@RandomDreamWalker2011/WIP-LOR
Sorry if I removed all of the code :stuck_out_tongue:

I have not looked at your code fully, but please do not use a global variable and changing in a procedure without the GLOBAL keyword,
I keep seeing people here using all these global variables and ignoring issue of scoping while it is always safer to never use global variables and if needed pass the as argument or return things to add to a global variable.

2 Likes

I don’t think I really use global. I think I use a function to return a value that is added to a variable that controls the loop.

1 Like

This is global, the best would be to have the hero init return self and add it to heroes_list in his scope and not the class.
Classes should be self contained except for constants.
I am not sure this is your problem, but it is also a possible problem,

For your problem try to add some prints in the battle loop to understand what conditions is triggering a loop break or return that should not happen.

1 Like

I run it through with a debugger, and the hero class is designed to catch errors though. However, it somehow glitches right out of the hero for loop and straight to the AI loop without any errors, and since the enemies are also an object of the Hero class, that probably isn’t the case. . . thanks for pointing this out, though.

1 Like

Skip the debugger and add prints in the battle loop of the type print(1), print(2) and so forth (with " of course) to monitor where the battle loop does not do what it is supposed to do.
Then print the variables and see what is causing it.

Old fashion debugging but it works always.

2 Likes

I did that and I think the problem is here:

               if over == 0:
                    break
                else:
                    return

This is included right after the hero action. If the battle isn’t over (AKA over =0), it will break out of the hero loop.
I’m not sure about wether this is the only problem in the code though. . .

Keep looking. Once the code is complex there can be little bugs needed this type of debugging,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.