Python User-Defined Global Variables Across Modules: Issues calling them when other modules are run -


i new python , learning work functions , multiple modules within python program.

i have 2 modules "functions_practice_main" (which runs menu) , "functions_practice" has code simple program works out if user's input divides 7 or not (i know...pretty dull practice).

what want user enter name when menu module runs , global variable displayed througout program make program more personal.

however, when run menu module asks name twice. first name entered displayed in 'divide 7' program , second name entered displayed in menu. understand why happening (due import of functions_practice module demanding know global variables in functions_practice_main module before rest of code has chance run) need know how fix this.

how can user enter name once when menu module runs , global variable displayed througout program make more personal user.

functions_practice_main

import functions_practice, sys   name = input("what name? ")  def main():  while true:    print(name, "'s menu")   print("*******************")   print("1. can divide 7?")   print("2. quit")   print("*******************")   selection = int(input("your number selection please..."))    if selection == 1:     functions_practice.main()   elif selection == 2:     sys.exit()   else:     print("your number selection invalid, please try again...")    if __name__ == '__main__':     main() 

*functions_practice*

import functions_practice_main  def inputdata(name):     print(name)     number = int(input(" please enter number: "))     return number  def processdata(number):     answer = number % 7     if answer == 0:         remainder = true     else:         remainder = false     return remainder  def outputdata(remainder):     if remainder == true:         print("the number entered doesn't have remainder")     elif remainder == false:         print("the number entered have remainder")    def main():     number = inputdata(functions_practice_main.name)     remainder = processdata(number)     outputdata(remainder)   if __name__ == '__main__':     main() 

running module script not count importing module. when functions_practice_main.py script imports functions_practice, , functions_practice imports functions_practice_main, python doesn't care code in functions_practice_main.py running main script. python run again produce module.

how fix this? well, there several things should do. first, avoid circular imports if @ possible. instead of having functions_practice import functions_practice_main, pass data functions_practice needs functions_practice_main function arguments.

in functions_practice_main:

functions_practice.interactive_remainder_test(name) 

in functions_practice:

def interactive_remainder_test(name):     number = inputdata(name)     remainder = processdata(number)     outputdata(remainder) 

second, things this:

name = input("what name? ") 

belong in file's main, since shouldn't run when file imported.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -