【发布时间】:2019-01-28 19:49:00
【问题描述】:
我试图编写一个程序/游戏,你必须在其中拆除炸弹。它里面有一个计时器,它会同时检查你是否剪断了正确的电线以使其解散。但是当我运行它时,它会在输入的同一行上两次打印出时间,如下所示:
只有第一个计时器会改变。然后光标跳到一个新行,如果我输入一些内容,它会在该行上(我希望它在同一行上)。当计时器完成时,代码也不会exit。
我怎样才能解决这个问题,以便计时器只在第一行,在第二行它要求输入,并且当计时器完成时它应该打印“炸弹爆炸”?
#import time library for the timer
import time
#import thread library to run timer and wire function at the same time
from threading import Thread
def splitTime(sec):
#splits seconds in minutes and seconds
seconds = sec % 60
minutes = int((sec - seconds) / 60)
if minutes == 0 and seconds == 0:
print("00:00")
else:
if minutes < 10 and seconds < 10:
print("0" + str(minutes) + ":" + "0" + str(seconds), end="\r")
elif minutes < 10:
print("0" + str(minutes) + ":" + str(seconds), end="\r")
elif seconds < 10:
print(str(minutes) + ":" + "0" + str(seconds), end="\r")
else:
print(str(minutes) + ":" + str(seconds), end="\r")
def timer():
#the timer
timeToSet = 10
while timeToSet >= 0:
splitTime(timeToSet)
time.sleep(1)
timeToSet -= 1
if timeToSet == 0:
print("The bomb went off!")
exit()
def wireFunction():
color = input("which wire to cut?: ")
if color == "red":
print("The bomb has been defused!")
exit()
else:
print("The bomb went off!")
exit()
wire = "red"
print("There are these wires: blue, red, green, black")
if __name__ == "__main__":
Thread(target = wireFunction).start()
Thread(target = timer).start()
【问题讨论】:
-
它确实不将时间写入“作为输入”。只是终端以这种方式打印它,但它不应该包含在用户编写的内容中
标签: python multithreading