How to change global variables in Python

python

Solution

To update global variables you could use

global ID
ID="Yes"

before assigning variable to ID = "YES"

But changing ID will be no effect on project variable, `project = ("Yep"+ID)`, because project is already a string

you need to make a function like

def getprojectname(ID):
    return project+ID

The whole program may be like this

UPDATE: ... removed

Problem

I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing this. Some test code is below to explain what I mean. ``` ID = 'No' project = ("Yep"+ID) # ID added with 'No' value which I later want to change def pro(): ID = "YES" print ID def pro1(ID): # I could declare project again to get this to work, but I would like to avoid this print project # I want this to print with the new ID number. if __name__ == '__main__': pro() pro1(ID) ``` Has anyone any ideas, thanks I have tried the `global` variable but when I do this the project variable still prints out `YepNo` instead of `YepYES`. I want the new variable from the function `pro`to change the variable in the `project` variable.

Original source

Related problems