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

2024-06-19 06:59:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用全局变量,但我不能做它没有任何错误。我不记得怎么完全使用全局变量了。如果你有什么建议,请告诉我。你知道吗

我的代码在下面

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()

Tags: oftheforinputisdefglobalinitial
1条回答
网友
1楼 · 发布于 2024-06-19 06:59:57

你们全球人的声明是错误的。你声明你的globals作为一个如何计算它们的描述。那是行不通的。python中的global关键字用于将变量引入局部范围。然后你可以改变它。像这样:

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

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

相关问题 更多 >