如何在Python中使用全局变量?

时间:2017-09-10 13:23:55

标签: python python-3.x

我正在使用全局变量,但我无法解决任何错误。我不记得如何完全使用全局变量。如果您有任何提示,请告诉我。

我的代码在

下面
print(" Greenfly Population Model")
    chocies=[]
    def Newgen():
        global GA=Gen0_A #duplicates adults numbers
        global GJ=Gen0_J #duplicates juvenile numbers
        global Gen0_J=(Gen0_A*Birth_rate) #calculates new number of juveniles
        global Gen0_A=(GJ*Srate_J) #calulates new number of adults
        global Gen0_S =((GA*Srate_A)+(Gen0_S*Srate_S)) #calculates new number of seniles
        global Total = Gen0_J+Gen0_A+Gen0_S #calculates total number of greenflies



def option1():
    if chocies==[]:
        print("Set the Generation 0 values!")
    else:
        print(chocies[:])
def option2():
    print("Display the Generation 0 values")

def option3():
    print("Run the model")

def option4():
    print("Export data")

def option5():
    print("Quit")
    time.sleep(1)
    print("GOOD BYE")
    time.sleep(1)
    # exiting the loop


print("""
1- Set the Generation 0 values
2- Display the Generation 0 values
3- Run the model
4- Export Data
5- Quit
""")#all different options


s = int(input("from the menu above please pick your choice"))
print("setting the generation 0 values")
i = int(input("Enter the number of generations you want the model to run for"))

G0_A = int(input("Choose adult survival rate between 0 and 1:"))
GJ = int(input("Choose Juvenile survival rate between 0 and 1:"))
G0_S  = int(input("Choose Senile survival rate between 0 and 1:"))
r = int(input("please enter the initial numbers of juvenile:"))
a = int(input("please enter the initial numbers of adults:"))
i = int(input("please enter the initial numbers of seniles:"))
v = int(input("please enter the initial numbers of the adults:"))

print(s)
print(i)
print(m)
print(o)
print(n)
print(r)
print(a)
print(i)
print(v)

print("Greenfly Population Model")
print("""
1- Set the Generation 0 values
2- Display the Generation 0 values
3- Run the model
4- Export Data
5- Quit
""")#all different options

l = int(input("Please select an option from the menu above:"))
print("Displaying the generation 0 values")
e = int(input("The number of new generations to model is:"))
n = int(input("The initial population for the adults is:"))
o = int(input("The initial population for the Seniles is:"))
v = int(input("The initial population for the Juveniles is:"))
o = int(input("The Birthrate for the adults is:"))
i = int(input("The Survival rate for the Adults is:"))
t = int(input("The survival rate for the seniles is:"))

print(l)
print(e)
print(n)
print(o)
print(v)
print(o)
print(i)
print(t)

print("Greenfly Population Model")
print("""
1- Set the Generation 0 values
2- Display the Generation 0 values
3- Run the model
4- Export Data
5- Quit
""")#all different options

int(input("please select an option from the menu above:"))

print("Opening the file")
text_file = open("read_it.txt", "r")
text_file.close()

1 个答案:

答案 0 :(得分:0)

你的全局变量的声明是错误的。您将全局变量声明为如何计算它们的描述。那将无济于事。 python中的global关键字用于在本地范围内引入变量。然后你可以改变它。像这样:

def updateGlobals():
    global global_variable
    global_variable = "new value"

但是,我会将它们的全局变量放在一个类中,并添加相应更新变量的函数。我认为人们称之为单身设计模式,但也许你在你的小项目中不需要这个。