[Python] Why do I get a "referenced before assignment" error when assigning to a global...

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    In Python, I'm getting the following error:

    UnboundLocalError: local variable 'total' referenced before assignment


    At the start of the file (before the function where the error comes from), I declare total using the global keyword. Then, in the body of the program, before the function that uses total is called, I assign it to 0. I've tried setting it to 0 in various places (including the top of the file, just after it is declared), but I can't get it to work.

    Does anyone see what I'm doing wrong?

    Continue reading...

Compartilhe esta Página