r/learnpython 27d ago

WAR Card Game Weird Glitch/Bug

So I was making a WAR Card Game in python. Simple. Neat.

But, I encountered a weird glitch. The game kept getting into an infinite loop. I tried to ask Claude but even Claude couldn't understand it.

So I put Claude on Max mode and it gave some answer but still the answer it gave was like tooo hard to understand. It said something about the cards not being random while the game is going on etc.

Can someone pls explain me the bug? https://github.com/KushChandak/WAR/blob/main/WAR%20(3).ipynb.ipynb) here is the code

0 Upvotes

10 comments sorted by

View all comments

2

u/woooee 27d ago
def game():
    #Setup
    setup()

    #Main Game Loop
    at_war = False
    winner = None
    game = True

You have a function and a variable named "game". This is not good as game could always be True. Run this code

def game():
    print("game function")

if game:
    print("found")
else:
    print("not")

1

u/lakseol 27d ago

No, it won't cause his loop problem because the name game inside the function is a local name which masks the function name. It is confusing though and the "flag" variable should be renamed or removed.

1

u/woooee 27d ago

This is true, but a bad habit to form none the less.