-1

How would it be possible to get the value of Accessme in the mainloop function?

def example():
    test=True
    while test:
       print("some stuff")
       if test2==True:
          Accessme = 400                   # Need to access this
          print("some stuff")
             if test3==True:
                print("some stuff")
                mainloop(x,y)
       elif test2==False:
          print("some stuff")

def mainloop(x,y):
   cheese = 1
   noise = []
   for something in somecodehere:
       print("some stuff") 
   output = some more code here
   print("some stuff",Accessme )          #Calling from here 

This is the error i get:

> NameError: name 'Accessme' is not defined
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • 1
    Any reason why you can't pass the variable as an argument to `mainloop` (which is the way to do this)? – MatsLindh Dec 12 '15 at 23:20
  • yeah. The code has sorta like a menu thing. And the value of Accessme is generated by what the user puts in, through this menu option thing. Similar to case-switch. It can't really be done another way – Jonathan Laliberte Dec 12 '15 at 23:22
  • I think @MatsLindh 's point was that you could add a third parameter to `mainloop` and pass `Accessme` to it in that parameter. – Scott Hunter Dec 12 '15 at 23:25
  • Ahh i see. I don't know if i can do that. This is what the mainloop looks like: def mainloop(a: str, b: str) -> str: – Jonathan Laliberte Dec 12 '15 at 23:26
  • "add a third parameter to `mainloop`": This means in both the definition *and* when calling it. – Scott Hunter Dec 12 '15 at 23:28
  • What should happen if the `Accessme = 400` line of code doesn't get reached? For that matter, how can this be expected to make any sense at all, if `example` hasn't been called yet? – Karl Knechtel Dec 25 '22 at 00:31

2 Answers2

3

Your example code was messy to word with so I simplified enough so you understand the concept:

def example():
    test=True
    while test:
       Accessme = 400 #Assign the variable
       break
    return Accessme #This will return the variable. Accessme is local to the function example and nowhere else. 

def mainloop(x=0,y=0):
    cheese = 1
    noise = []
    print("some stuff",example() ) #You can grab the output from example by calling the function example, not Accessme.

mainloop()

I advise you read up on Scope. Your issue was Accessme is not in the scope of mainloop.

Community
  • 1
  • 1
abe
  • 504
  • 4
  • 13
  • And to add, global variables are generally bad practice, I do not advise you use them if you do not explicitly have to for example: setting a constant like G = 9.81 because here on earth, at least to our knowledge, this gravity constant will not change. – abe Dec 12 '15 at 23:32
1

If you want access to Accessme to be global (that is, outside of any particular function), then you need to tell each function that this is the case:

global Accessme

Use of globals is usually in bad style. If you want to get information out of a function, it would be better return that information, just as getting information into a function is best done by a parameter.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101