以下是一个基于 Python 的小程序,可以随机循环单词并且可以暂停,同时允许自定义词库。

import random
import time

# 自定义词库
words = ['apple', 'banana', 'orange', 'peach', 'pear']

# 选择随机单词
def choose_word():
    return random.choice(words)

# 随机循环单词
def random_loop():
    while True:
        word = choose_word()
        print(word)
        time.sleep(1)

# 主函数
def main():
    print('Press ENTER to pause or resume the loop.')
    print('Press Ctrl+C to exit.')
    try:
        while True:
            # 开始循环
            print('Starting loop...')
            random_loop()
    except KeyboardInterrupt:
        # 捕获Ctrl+C,退出程序
        print('Exiting...')
        exit()

if __name__ == '__main__':
    main()

使用方法:

  1. 将以上代码复制到 Python 环境中,保存为一个 Python 文件(例如 random_word.py)。
  2. 在代码中自定义词库,添加或删除单词。
  3. 运行程序,将会开始随机循环单词。
  4. 按下回车键可以暂停或恢复循环。
  5. 按下 Ctrl+C 可以退出程序。
Python 随机循环单词小程序:自定义词库,暂停功能

原文地址: https://www.cveoy.top/t/topic/nWpD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录