[Python] Is there a way to change a global variable permanently from inside a function...

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    Recently i have been trying to make a tbag game on python, and I have got quite far into the process, but for the quest system, I want a variable that can reflect a quest, and tell the computer if it had been completed yet. The problem I am having is that I cannot find where to call the variable, if I call it outside of the function as a global variable, that I know of, I can't change it. If I call it inside if the function then it will reset everyone the function is called.

    The two variations that I tried where:

    global questdone
    questdone = 0
    def questarea1():
    if questdone==1:
    #area's code runs
    else:
    #run quest code
    questdone=1


    def questarea1():
    questdone=0
    if questdone==1:
    #area's code runs
    else:
    #run quest code
    questdone=1


    My question got marked as a duplicate but the code from the linked question does not work in my scenario, so i modified it and got the same error I already had, presumably because the variable is used in the if, elif before being made global (this is the modified code and error) and I am still unsure on what to do to fix this

    Continue reading...

Compartilhe esta Página