这是我previous question的续篇,有更多细节。 (格式化应该更好,因为我现在在电脑上!)
所以我正在尝试用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上提问时我很新!
答案 0 :(得分:1)
一般情况下,您无法同时运行两项内容,因此当您位于while True
的{{1}}时,您永远无法访问代码的下一位,因为start()
是真的很好。
所以,线程来救援!线程允许你在一个地方进行一件事,而在另一个地方进行另一件事。如果我们将代码更新并在其自己的线程中打印温度,我们可以设置该运行然后继续并在while True
开始执行无限循环,同时我们的线程在后台运行。
另外请注意,您应该导入start()
本身,而不是从coretemp
导入变量,否则您将在coretemp
中使用变量ct
的副本当您确实想要使用main.py
中的ct
的实际值时。
无论如何,这里是对代码的最小更新,显示了线程的使用。
coretemp
:
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
你可以看到我们还有一些工作要做,以使输出看起来不错,等等,但希望你能得到一般的想法。