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