a'的倒计时循环;原始输入';在Python的同一行上
我想为一个正常的a'的倒计时循环;原始输入';在Python的同一行上,python,for-loop,input,countdown,Python,For Loop,Input,Countdown,我想为一个正常的原始输入做一个倒计时循环如果正常的原始输入不改变,只改变数字(在原始输入) 那么,你想再试一次吗?[15-1]在一条线上输出,数字只会改变 这是我到目前为止所做的,但它不起作用。那我该怎么做呢 while True: for i in range(15,-1,-1): con1=raw_input("\n Do you want to try again? " + str(i,)) if i == 0
原始输入做一个倒计时循环
如果正常的原始输入
不改变,只改变数字(在原始输入
)
那么,你想再试一次吗?[15-1]在一条线上输出,数字只会改变
这是我到目前为止所做的,但它不起作用。那我该怎么做呢
while True:
for i in range(15,-1,-1):
con1=raw_input("\n Do you want to try again? " + str(i,))
if i == 0:
print "\n Thanks for playing!"
exit(0)
elif con1 == "no":
print "\n Thanks for playing!"
time.sleep(3)
exit(0)
elif con1 == "yes":
break
Linux答案--在Windows上不起作用
Python 3
Python 2
getans
将在超时时返回None
,否则返回响应。理论上,如果Windows版本的可以实现输入,这可以在Windows上运行,但我没有测试过。@wei2912更新的问题这是一个很难解决的问题,通常-原始输入
真的无法做到这一点,你需要使用类似的工具。在raw_input
下使用线程
/信号
可能会有黑客攻击,但是。。无论哪种方式,你都会进入兔子洞。这太完美了。非常感谢。是的,我正在为Linux开发。
import select
import sys
def has_input(timeout=0.0):
return select.select([sys.stdin], [], [], timeout)[0]
def getans(timeout=15):
i = timeout
max_num_length = len(str(timeout))
while i:
print("\rDo you want to try again? {:{}} ".format(i, max_num_length),
end="", flush=True)
i -= 1
if has_input(1):
return input()
print()
return None
print(getans())
import select
import sys
def has_input(timeout=0.0):
return select.select([sys.stdin], [], [], timeout)[0]
def getans(timeout=15):
i = timeout
max_num_length = len(str(timeout))
while i:
sys.stdout.write("\rDo you want to try again? {:{}} ".format(i, max_num_length))
sys.stdout.flush()
i -= 1
if has_input(1):
return raw_input()
print
return None
print getans(5)