<分区>
我正在尝试更改程序中的变量。我在程序开始时声明了一个全局变量,我想在程序的不同函数中更改该变量。我可以通过再次在函数内声明变量来做到这一点,但我想知道是否有更好的方法来做到这一点。下面是一些测试代码来解释我的意思。
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)
有没有人有什么想法,谢谢
我已经尝试了 global
变量,但是当我这样做时,项目变量仍然打印出 YepNo
而不是 YepYES
。我想要函数 pro
中的新变量来更改 project
变量中的变量。