Python 随机循环单词程序,支持暂停和自定义词库
这是一个使用 Python 编写的随机循环单词程序,可以自定义词库,并且支持暂停功能。
import random
import time
words = ['apple', 'orange', 'banana', 'pear'] # 自定义词库
while True:
random_word = random.choice(words) # 随机选择一个单词
print(random_word)
time.sleep(1) # 暂停1秒
# 等待用户输入
print('输入'p'暂停,输入'c'继续,输入'q'退出')
user_input = input()
# 处理用户输入
if user_input == 'p':
print('已暂停,输入'c'继续')
while True:
user_input = input()
if user_input == 'c':
print('已继续')
break
elif user_input == 'q':
print('已退出')
break
在这个程序中,我们首先定义了一个自定义词库,它包含了四个单词:'apple'、'orange'、'banana' 和 'pear'。然后我们用一个无限循环来随机选择单词,并输出它。接着用 time.sleep(1) 来暂停1秒。
在每个单词输出后,我们让程序等待用户输入。如果用户输入了'p',则程序会进入暂停状态,等待用户输入'c'来继续。如果用户输入了'q',则程序会退出循环,结束程序。
这个程序可以很容易地扩展,你可以添加更多的单词到词库中,或者修改暂停时间,让它更符合你的需求。
原文地址: https://www.cveoy.top/t/topic/nWqw 著作权归作者所有。请勿转载和采集!