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?

5
  • 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 Commented Jul 12, 2020 at 20:51
  • Did you import the module? Why do you expect checkbuy to be in scope in the other module? Commented Jul 12, 2020 at 20:51
  • See also this question. 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). Commented Jul 12, 2020 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. Commented Jul 12, 2020 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. Commented Jul 12, 2020 at 22:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.