How to use global variables in Python? -


i using global variables can't out errors. can't remember how use global variables. if have tips please tell me.

my code below

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 generation 0 values!")     else:         print(chocies[:]) def option2():     print("display generation 0 values")  def option3():     print("run model")  def option4():     print("export data")  def option5():     print("quit")     time.sleep(1)     print("good bye")     time.sleep(1)     # exiting loop   print(""" 1- set generation 0 values 2- display generation 0 values 3- run model 4- export data 5- quit """)#all different options   s = int(input("from menu above please pick choice")) print("setting generation 0 values") = int(input("enter number of generations want model run for"))  g0_a = int(input("choose adult survival rate between 0 , 1:")) gj = int(input("choose juvenile survival rate between 0 , 1:")) g0_s  = int(input("choose senile survival rate between 0 , 1:")) r = int(input("please enter initial numbers of juvenile:")) = int(input("please enter initial numbers of adults:")) = int(input("please enter initial numbers of seniles:")) v = int(input("please enter initial numbers of 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 generation 0 values 2- display generation 0 values 3- run model 4- export data 5- quit """)#all different options  l = int(input("please select option menu above:")) print("displaying generation 0 values") e = int(input("the number of new generations model is:")) n = int(input("the initial population adults is:")) o = int(input("the initial population seniles is:")) v = int(input("the initial population juveniles is:")) o = int(input("the birthrate adults is:")) = int(input("the survival rate adults is:")) t = int(input("the survival rate 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 generation 0 values 2- display generation 0 values 3- run model 4- export data 5- quit """)#all different options  int(input("please select option menu above:"))  print("opening file") text_file = open("read_it.txt", "r") text_file.close() 

the declaration of globals wrong. declare globals description of how should calculated. not work. global keyword in python used bring variable in local scope. can alter it. this:

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

however, put them globals in class , add functions update variables accordingly. think people call singleton design pattern, maybe don't need in little project.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -