简体   繁体   English

跨模块的Python用户定义的全局变量:运行其他模块时调用它们的问题

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

I am fairly new to Python and currently learning to work with functions and multiple modules within a python program. 我对Python相当陌生,目前正在学习在python程序中使用函数和多个模块。

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). 我有两个模块“ Functions_Practice_Main”(运行菜单)和“ Functions_Practice”,其中包含一个简单程序的代码,该程序可以计算出用户的输入是否除以7(我知道...相当愚蠢的做法)。

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. 输入的名字显示在“ 7分频”程序中,输入的名字显示在菜单中。 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. 我知道为什么会发生这种情况(由于导入了Functions_Practice模块,要求在其余代码有机会运行之前先知道Functions_Practice_Main模块中的全局变量是什么),但我确实需要知道如何解决此问题。

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. 运行菜单模块时,如何使用户输入名称ONCE,然后通过显示整个程序来获取此全局变量,以使其对用户更具个性。

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()

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. 当Functions_Practice_Main.py脚本导入Functions_Practice,而Functions_Practice导入Functions_Practice_Main时,Python不在乎Functions_Practice_Main.py中的代码已作为主脚本运行。 Python will run it again to produce the module. Python将再次运行它以生成模块。

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. 而不是让Functions_Practice导入Functions_Practice_Main,而是将来自Functions_Practice_Main的所有数据Functions_Practice需要作为函数参数。

In Functions_Practice_Main: 在Functions_Practice_Main中:

Functions_Practice.interactive_remainder_test(name)

In Functions_Practice: 在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. 属于文件main ,因为它们在导入文件时不应运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM