我正在做一个游戏,我需要从另一个文件中的函数打印一个变量。我尝试将它们设置为全局,但仍然会出现错误。
功能文件:
from Computers import YourComputer
from Computers import Computer1
def StartNetwork():
global routerIP
global username
global password
routerIP = "192.168.0.0"
username = "admin"
password = "password"
脚本:
from Network import StartNetwork
def Terminal():
x = 1
while x == 1:
cmd = input("> ")
if cmd == "network_info":
print(routerIP)
错误:
文件“ C:\ Users \ user \ Desktop \ PythonGame \ Terminal.py”,第9行,在终端中 打印(routerIP) NameError:名称“ routerIP”未定义
答案 0 :(得分:2)
这可以使用Python中的类来实现
文件-x
class x:
routerIP = "192.168.0.0"
username = "admin"
password = "password"
def startnetwork(self):
pass
文件-y
from x import x
def Terminal():
a = x()
print(x.routerIP)
Terminal()
运行文件:y.py
输出:192.168.0.0