使用模块中另一个 Python 文件和函数的变量不是 运行?

Using variables from another Python file & function in module not running?

这是我的 previous question 的延续,包含更多详细信息。 (格式化应该更好,因为我现在在电脑上!)

所以我正在尝试在 Python 中创建一个游戏,如果数字达到一定数量,您就会输。你试图控制这个数字,以防止它达到它不应该达到的数字。现在,我在上一个问题中有一个错误:

AttributeError: module 'core temp' has no attribute 'ct'

不过,我稍微修改了我的代码,不再出现任何错误。但是,当我 运行 代码时,我创建的模块中的函数不会 运行.

为确保任何试图找出解决方案的人都拥有他们需要的所有资源,我将提供我的所有代码。

这是文件中的代码main.py:

from termcolor import colored
from time import sleep as wait
import clear
from coretemp import ct, corestart

print(colored("Begin program", "blue"))
wait(1)
clear.clear()


def start():
    while True:
        while True:
            cmd = str(input("Core> "))
            if cmd == "help":
                print(
                    "List of commands:\nhelp - Show a list of commands\ntemp - Show the current temperature of the core in °C\ninfo - Show core info\ncool - Show coolant control"
                )
                break
            elif cmd == "temp":
                if ct < 2000:
                    print(
                        colored("Core temperature: " + str(ct) + "°C",
                            "green"))
                elif ct < 4000:
                    print(
                        colored("Core temperature: " + str(ct) + "°C",
                            "yellow"))
                elif ct >= 3000:
                    print(
                        colored("Core temperature: " + str(ct) + "°C", "red"))


print("Welcome to SprinkleLaboratories Main Core System")
wait(1)
print(
    "\nYou have been hired as the new core operator.\nYour job is to maintain the core, and to keep the temperature at a stable number. Or... you could see what happens if you don't..."
)
wait(3)
print("\nTo view available commands, use the \"help\" command!")
cont = input("Press enter to continue> ")
clear.clear()
start()
corestart(10)

这是文件中的代码clear.py:

print("clear.py working")

def clear():
print("\n"*100)

这是文件中的代码coolant.py:

from time import sleep as wait

print("coolant.py working")

coolam = 100
coolactive = False

def coolact():
    print("Activating coolant...")
    wait(2)
    coolactive = True
    print("Coolant activated. "+coolam+" coolant remaining.")

这是文件中的代码coretemp.py:

from coolant import coolactive
from time import sleep as wait
print("coretemp.py working")

ct = 0

def corestart(st):
  global ct
  ct = st
  while True:
    if coolactive == False:
      ct = ct + 1
      print(ct)
      wait(.3)
    else:
      ct = ct - 1
      print(ct)
      wait(1)

注:

文件中的一些代码不完整,所以有些东西目前看起来什么都没做

如果您想查看代码本身,这里是 link 到 repl.it: Core

注意#2:

抱歉,如果格式不正确,如果我在问题中做错了什么等等。我对在 Whosebug 上提问还很陌生!

您通常不能同时拥有两个东西 运行,所以当您处于 start()while True 时,您永远不会进入代码的下一部分,因为while True永远如此。

所以,线程 来拯救!线程允许你在一个地方做一件事,在另一个地方做另一件事。如果我们将更新和打印温度的代码放在它自己的线程中,我们可以设置 运行 然后继续并在 start() 中开始执行无限循环,同时我们的线程保持 运行在后台。

另外请注意,您应该导入 coretemp 本身,而不是从 coretemp 导入变量,否则您将在 [=19] 中使用变量 ct 的副本=] 当你真的想使用来自 coretemp.

ct 的实际值时

无论如何,这是对显示线程使用的代码的最小更新。

main.py:

from termcolor import colored
from time import sleep as wait
import clear
import coretemp
import threading

print(colored("Begin program", "blue"))
wait(1)
clear.clear()


def start():
    while True:
        while True:
            cmd = str(input("Core> "))
            if cmd == "help":
                print(
                    "List of commands:\nhelp - Show a list of commands\ntemp - Show the current temperature of the core in °C\ninfo - Show core info\ncool - Show coolant control"
                )
                break
            elif cmd == "temp":
                if coretemp.ct < 2000:
                    print(
                        colored("Core temperature: " + str(coretemp.ct) + "°C",
                                "green"))
                elif coretemp.ct < 4000:
                    print(
                        colored("Core temperature: " + str(coretemp.ct) + "°C",
                                "yellow"))
                elif coretemp.ct >= 3000:
                    print(
                        colored("Core temperature: " + str(coretemp.ct) + "°C", "red"))



print("Welcome to SprinkleLaboratories Main Core System")
wait(1)
print(
    "\nYou have been hired as the new core operator.\nYour job is to maintain the core, and to keep the temperature at a stable number. Or... you could see what happens if you don't..."
)
wait(3)
print("\nTo view available commands, use the \"help\" command!")
cont = input("Press enter to continue> ")
clear.clear()
coretemp.corestart(10)
t1 = threading.Thread(target=coretemp.coreactive)
t1.start()
start()

coretemp.py:

from coolant import coolactive
from time import sleep as wait

print("coretemp.py working")

ct = 0

def corestart(st):
  global ct
  ct = st

def coreactive():
  global ct
  while True:
    if coolactive == False:
      ct = ct + 1
      print(ct)
      wait(.3)
    else:
      ct = ct - 1
      print(ct)
      wait(1)

您可以看到我们还有一些工作要做,以使输出看起来更漂亮等等,但希望您能大致了解。