Hello,
I have this functions:
main(), menu1(),menu2() and so on. Main function takes an input and call one of the menu() functions so ... menu1(),menu2() functions needs to be defined before the main() function. The problem appears when i want to call the main() function inside one of the menu() functions. ”NameError: name 'main' is not defined” because main() is defined after those functions.. How can I solve this ?

I hope that you understand what's my problem:D

Thanx in advance !

Please post a sample code, and I will try to help you out.

... and if it is really main function you should call it only once from end of the module.

#!/usr/bin/python

def menu1():
  from dictionary import dmenu1 
  print "Add into the list the result of a new participant."
  print "\ta to adds the score x for the last participant."
  print "\tb to inserts score x at position y in te list."
  print "\tz to go back at the main menu."
  m=int(input())
  return dmenu1[m]()


def menu2():
    print "smt"

def menu3():
    print "smt"
def menu4():
    print "smt"
def menu5():
    print "smt"
def menu6():
    print "smt"

from dictionary import dmain
def main():
  print "Enter: "
  print "\t1 to add into the list the result of a new participant."
  print "\t2 to modify the scores from the list."
  print "\t3 to write the participants whose score has different properties."
  print "\t4 to obtain different characteristics of participants."
  print "\t5 to filter scores."
  print "\t6 to undo the last operation."
  n=int(input())
  return dmain[n]() 

And this is dictionary.py

#!/usr/bin/python
from menu import*
dmain = {1: menu1,
         2: menu2,
         3: menu3,
         4: menu4,
         5: menu5,
         6: menu6
    }
dmenu1 = {1: 'option',
          2: 'option',
          3: main
          }

Try this to avoid cyclical imports ...

#!/usr/bin/python
# save as menu.py

def menu1():
  from dictionary import dmenu1 
  print "Add into the list the result of a new participant."
  print "\ta to adds the score x for the last participant."
  print "\tb to inserts score x at position y in te list."
  print "\tz to go back at the main menu."
  m=int(input())
  return dmenu1[m]()

def menu2():
    print "smt"

def menu3():
    print "smt"
def menu4():
    print "smt"
def menu5():
    print "smt"
def menu6():
    print "smt"

def main():
  print "Enter: "
  print "\t1 to add into the list the result of a new participant."
  print "\t2 to modify the scores from the list."
  print "\t3 to write the participants whose score has different properties."
  print "\t4 to obtain different characteristics of participants."
  print "\t5 to filter scores."
  print "\t6 to undo the last operation."
  n=int(input())
  return dmain[n]()

if __name__ == '__main__':
    from dictionary import dmain

    main()

Well, I had to import dmain before return dmainn on line 34 and it works!

Can you explain me shortly how that if statement works?
Thank you very much!

Can you explain me shortly how that if statement works?

When a python file foo.py is executed, a variable __name__ is inserted in its namespace. If foo.py is ran as the main script, the value of this variable is the string "__main__". If instead foo.py is imported from another script with import foo, the value of this variable is the module's name "foo". For example

>>> from xml.dom import minidom
>>> minidom.__name__
'xml.dom.minidom'

I have another problem. I have a function which takes an argument. That function i need to call it from dmain1 dictionary, something like this:
dmain1 = {1: menu1_a(param), 2: 'option', 3} How can I do this?

See lambda statement or functools.partial

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.