1
我正在尋找一種方法,當用戶按[空格鍵]時,暫停下面的for循環,然後再次按[空格鍵]時從最近一次迭代繼續循環。Python - 使用[空格鍵]暫停循環
目前,腳本會提示用戶輸入三個值,然後按定時間隔從文本文件中打印單詞,直到沒有剩餘單詞。
什麼是最好的方式去做這件事?非常感謝。
import time
with open('Textfile.txt', 'r', encoding='utf8') as file:
data = file.read()
data2 = data.split()
def reading(start, speed, chunks):
for i in range(start, len(data2), chunks):
print('\r' + (' '.join(data2[i:i+chunks])), end="")
time.sleep(60/speed * chunks)
print ("The End.")
start = int(input('Where would you like to start? (word number) '))
speed = int(input('How many words per minute? '))
chunks = int(input('How many words at a time? '))
reading(start, speed, chunks)
看看[curses編程](https://docs.python.org/3/howto/curses.html)。 – eryksun 2014-11-25 17:18:09