Python 当我试图同时运行这两个函数时,似乎无法得到任何响应
我正在为我的python类做最后一个项目,我一直在拼命地尝试让这两个进程同时运行,但是当我运行它们时,我没有得到任何函数的响应Python 当我试图同时运行这两个函数时,似乎无法得到任何响应,python,python-3.x,Python,Python 3.x,我正在为我的python类做最后一个项目,我一直在拼命地尝试让这两个进程同时运行,但是当我运行它们时,我没有得到任何函数的响应 import threading, time count = 0 clock = 1200 - count def f1(): while True: if count < 1200: time.sleep(1) count += 1 print(count)
import threading, time
count = 0
clock = 1200 - count
def f1():
while True:
if count < 1200:
time.sleep(1)
count += 1
print(count)
else:
exit()
def f2():
pass
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
导入线程,时间
计数=0
时钟=1200-计数
def f1():
尽管如此:
如果计数小于1200:
时间。睡眠(1)
计数+=1
打印(计数)
其他:
退出()
def f2():
通过
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
抱歉,如果这是一个愚蠢的问题,我只是似乎无法让任何东西工作,我只是想能够从计时器的第一个功能从第二个功能在发挥参考。
另外,为了提供一些上下文,在第二个函数中,我创建了一个逃生室,它将使用第一个函数来检查他们还有多少时间可以逃生
基本上,现在我在函数的第二部分加入了一个部分来测试,看看是否可以将信息从第一个函数拉到第二个函数。这一次,两个程序同时运行得很好,但由于我在python方面的noob级知识,我不确定如何从我的第一个函数中获得count的更新版本
import threading, time
count = 0
def f1():
global count
while True:
if count < 1200:
time.sleep(1)
count += 1
print(count)
else:
exit()
return count
def f2():
clock = 1200 - count
p = 1
while True:
while p == 1:
if p == 1:
print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>")
print("It seems you wont be able to get any clear information without inspecting each thing")
print("<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>")
time.sleep(5)
desk = input("What do you do?")
if desk.lower() == ("inspect clock"):
print("You look up at the digital clock on the wall.")
print("Glowing red, the clock continues to count down.")
print("You seem to have", clock," seconds left")
elif desk.lower() == ("inspect box"):
print("After clear inspection of the box, you note that each of the buttons are a color of the rainbow")
print("However, you also notice that each button has a number written on it.")
print("The numbers and colors are as follows:")
print("'Red, 4 / Orange, 3 / Yellow, 6 / Green, 5 / Blue, 2 / Purple, 1")
elif desk.lower() == ("inspect paper"):
import Rainbow
print("Click the window to stop looking at the paper")
elif desk.lower() == ("get up"):
print("You find it impossible to move due to the chains wrapped around your legs.")
print("There is also a lock sitting on your lap, preventing you from removing the chain.")
elif desk.lower() == ("inspect lock"):
print("It is a lock, no key attached though")
elif desk.lower() ==("solve box"):
box = input("What is the solution? R=4,O=3,Y=6,G=5,B=2,P=1")
if box.lower() == ("43621"):
p = 2
else:
print("The box does nothing, mustve been the wrong combination")
else:
print("Invalid input. Retry.")
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
导入线程,时间
计数=0
def f1():
全局计数
尽管如此:
如果计数小于1200:
时间。睡眠(1)
计数+=1
打印(计数)
其他:
退出()
返回计数
def f2():
时钟=1200-计数
p=1
尽管如此:
而p==1:
如果p==1:
打印(“”)
打印(“似乎你不检查每件东西就无法获得任何清晰的信息”)
打印(“”)
时间。睡眠(5)
桌面=输入(“你做什么?”)
如果desk.lower()==(“检查时钟”):
打印(“你抬头看墙上的数字钟。”)
打印(“发光的红色,时钟继续倒数。”)
打印(“您似乎有”,时钟,“还剩秒”)
elif desk.lower()==(“检查盒”):
打印(“在对框进行清晰检查后,您注意到每个按钮都是彩虹色的”)
打印(“但是,您也注意到每个按钮上都写有一个数字。”)
打印(“数字和颜色如下:”)
打印(“红色、4/橙色、3/黄色、6/绿色、5/蓝色、2/紫色、1”)
elif desk.lower()==(“检查纸张”):
进口彩虹
打印(“单击窗口停止查看纸张”)
elif desk.lower()==(“起来”):
打印(“由于腿上缠着锁链,你无法移动。”)
打印(“膝盖上还有一把锁,防止你取下链条。”)
elif desk.lower()==(“检查锁”):
打印(“这是一把锁,但没有附带钥匙”)
elif desk.lower()==(“解算框”):
框=输入(“解决方案是什么?R=4,O=3,Y=6,G=5,B=2,P=1”)
如果box.lower()==(“43621”):
p=2
其他:
打印(“该框不起作用,一定是错误的组合”)
其他:
打印(“无效输入。重试”)
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
基本上,我希望能够使用输入“检查时钟”,并从定时器功能获得更新的计数。这可能是一个愚蠢的问题,如果是,请道歉。您需要让Python知道您指的是
pass
中的全局计数。为此,只需使用global
关键字。试试这个:
import threading, time
count = 0
clock = 1200 - count
def f1():
global count
while True:
if count < 1200:
time.sleep(1)
count += 1
print(count)
else:
exit()
def f2():
pass
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
全局变量是值得避免的,但下面的内容至少证明了大部分代码是正确的。我不确定你想用时钟做什么,所以我把它注释掉了。但这会返回两个函数的输出
import threading, time
count = 0
# clock = 1200 - count
def f1():
global count
while True:
if count < 1200:
time.sleep(1)
count += 1
print(count)
else:
exit()
def f2():
global count
while True:
print('f2():',count)
time.sleep(1)
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
导入线程,时间
计数=0
#时钟=1200-计数
def f1():
全局计数
尽管如此:
如果计数小于1200:
时间。睡眠(1)
计数+=1
打印(计数)
其他:
退出()
def f2():
全局计数
尽管如此:
打印('f2():',计数)
时间。睡眠(1)
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()
您有一个本地和全局版本的count
变量。实际上,我在30秒前刚刚实现了这一点,我很迟钝。另外,现在我有一个问题,我似乎无法从第一个函数中访问count变量,以便在第二个函数中使用。我希望您能看到这一点,但我在编辑中加入了时钟变量的用途,我似乎不知道如何获取第二个函数的更新计数。您的编辑有很多问题,但您在所用行中对时钟的引用将起作用。
import threading, time
count = 0
# clock = 1200 - count
def f1():
global count
while True:
if count < 1200:
time.sleep(1)
count += 1
print(count)
else:
exit()
def f2():
global count
while True:
print('f2():',count)
time.sleep(1)
threading.Thread(target=f1).start()
threading.Thread(target=f2).start()