0

I have a program that writes a separate in order to make a series of similar functions. These functions are to call another function, checkbuy, from the main source code. Whenever I try this, it says it's not defined. Globalizing the main function in the module functions fails.

Module function:

def buyMagikarp():
     global pokenums
     if checkbuy(10,1): #calling the function from main file
          pokenums['Magikarp']+=1
          Magikarp.config(text='Magikarp: '+str(pokenums['Magikarp']))

This function is used inside of a Tkinter Button object, and is successfully called when the button is clicked.

Function in main:

def checkbuy(price,amount):
    global coin, coingainnum
    if coin >= price:
        coin-=price
        updatecoin()
        coingainnum+=amount
        return True
    else:
        return False

Module function works in works when in main.

How to call function from function in local namespace?

  • 1
    You would need to import it. I'm guessing that `main` already imports the module, so you can't also import `main` from the module because that would be circular. I would move `checkbuy` to another module (`lib` or something more descriptive), which you can then import in both `main` and the other modules – nog642 Jul 12 '20 at 20:51
  • Did you import the module? Why do you *expect* `checkbuy` to be in scope in the other module? – juanpa.arrivillaga Jul 12 '20 at 20:51
  • See also [this question](https://stackoverflow.com/questions/6011371/python-how-can-i-use-variable-from-main-file-in-module). Although it's about a variable not a function; so the solution of passing the variable as a parameter doesn't really apply here (you could do it but **don't**). – nog642 Jul 12 '20 at 20:54
  • I am trying to write a series of similar functions based off a list. The module function was an example of one of these. That's why I need the separate module. – Purple UniTurtle Man Jul 12 '20 at 20:59
  • I moves the required functions and variables to the module and that fixed it. I was just hoping there was another way. – Purple UniTurtle Man Jul 12 '20 at 22:45

0 Answers0