-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
6
  • 1
    Any reason why you can't pass the variable as an argument to mainloop (which is the way to do this)? Commented Dec 12, 2015 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 Commented Dec 12, 2015 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. Commented Dec 12, 2015 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: Commented Dec 12, 2015 at 23:26
  • "add a third parameter to mainloop": This means in both the definition and when calling it. Commented Dec 12, 2015 at 23:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

Thanks! I think that is what i need. Will try now. Also, I already tried implementing it so that Accessme would return the value.. But i still get the same error.
Accessme is a variable; it can't return anything.
I realize that. This is pseudocode. With the working code the value of Accessme is derived through the return of a function that takes two parameters..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.