0

这是我之前的问题的延续,有更多细节。(格式化应该更好,因为我现在在电脑上!)

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

AttributeError:模块 'core temp' 没有属性 '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)

笔记:

文件中的一些代码不完整,所以有些事情现在看起来好像什么都不做

如果你想查看代码本身,这里有一个 repl.it 的链接: Core

笔记2:

对不起,如果事情的格式不正确,如果我在问题中做错了什么等等。我对在 Stackoverflow 上提问还很陌生!

4

1 回答 1

1

你通常不能同时运行两件事情,所以当你在其中时while Truestart()你永远不会进入下一段代码,因为永远while True都是如此。

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

另一个注意事项,您应该导入coretemp自身,而不是导入变量 from ,否则当您实际想要使用from的实际值时,您将使用变量的coretemp副本。ctmain.pyctcoretemp

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

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)

你可以看到我们还有一些工作要做,以使输出看起来不错等等,但希望你能得到大致的想法。

于 2018-05-16T15:11:29.220 回答