Question

I am fairly new to Python and currently learning to work with functions and multiple modules within a python program.

I have two modules "Functions_Practice_Main" (which runs a menu) and "Functions_Practice" which has the code for a simple program that works out if a user's input divides by 7 or not (I know...pretty dull practice).

What I want to do is get the user to enter their name when the menu module is runs and then get this global variable to by displayed througout the program to make the program more personal.

However, when I run the menu module it asks for a name twice. The first name entered is displayed in the 'divide by 7' program and the second name entered is displayed in the menu. I understand why this is happening (due to the import of the Functions_Practice module demanding to know what the global variables are in the Functions_Practice_Main module before the rest of the code has a chance to run) BUT I REALLY NEED TO KNOW HOW TO FIX THIS.

How can I get the user to enter their name ONCE when the menu module is runs and then get this global variable to by displayed througout the program to make it more personal for the user.

Functions_Practice_Main

import Functions_Practice, sys


name = input("What is your name? ")

def main():

while True:

  print(name, "'s Menu")
  print("*******************")
  print("1. Can you divide by 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 was invalid, please try again...")


 if __name__ == '__main__':
    main()

*Functions_Practice*

import Functions_Practice_Main

def inputData(name):
    print(name)
    number = int(input(" Please enter your 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 you entered doesn't have a remainder")
    elif remainder == False:
        print("The number you entered does have a remainder")



def main():
    number = inputData(Functions_Practice_Main.name)
    remainder = processData(number)
    outputData(remainder)


if __name__ == '__main__':
    main()
Was it helpful?

Solution

Running a module as a script does not count as importing it as a module. When the Functions_Practice_Main.py script imports Functions_Practice, and Functions_Practice imports Functions_Practice_Main, Python doesn't care that the code in Functions_Practice_Main.py is already running as the main script. Python will run it again to produce the module.

How do you fix this? Well, there are several things you should do. First, avoid circular imports if at all possible. Instead of having Functions_Practice import Functions_Practice_Main, pass any data Functions_Practice needs from Functions_Practice_Main as 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 like this:

name = input("What is your name? ")

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top