# ===== CodeLab: game-python-typing ===== # 以下代码片段按文章出现顺序拼接, 共 6 段 # ----- 片段 1 (text) ----- 第 1/10 个词: python 输入: python [#---------] 1/10 第 2/10 个词: loop 输入: lopp [##--------] 2/10 # ----- 片段 2 (python) ----- import random WORDS = [ 'python', 'function', 'variable', 'loop', 'dictionary', 'algorithm', 'compile', 'debug', 'module', 'recursion', 'terminal', 'keyboard', 'practice', 'speed', 'accuracy', 'language', 'console', 'integer', 'string', 'import', 'return', 'while', 'break', 'error', 'output', ] TOTAL = 10 # 本轮要打的单词数 random.shuffle(WORDS) print(WORDS[:TOTAL]) # ----- 片段 3 (python) ----- import random import sys WORDS = ['python', 'loop', 'debug', 'string', 'import', 'error', 'while', 'break', 'speed', 'accuracy'] TOTAL = 10 def pick_words(): random.shuffle(WORDS) return WORDS[:TOTAL] def show_progress(done, total): bar = '#' * done + '-' * (total - done) sys.stdout.write(f'\r[{bar}] {done}/{total}') sys.stdout.flush() words = pick_words() print('本轮单词:', words) show_progress(3, TOTAL) print() # ----- 片段 4 (python) ----- import time def compare(target, typed): """逐字符比对,返回 (正确数, 错误数)""" correct = wrong = 0 for i in range(max(len(target), len(typed))): if i < len(target) and i < len(typed) and target[i] == typed[i]: correct += 1 else: wrong += 1 return correct, wrong print(compare('python', 'pythn')) # 漏了一个 o print(compare('python', 'pythonx')) # 多打了一个 x start = time.time() time.sleep(0.05) # 模拟打字耗时 print(f'耗时 {time.time() - start:.2f} 秒') # ----- 片段 5 (python) ----- correct_chars, wrong_chars = 48, 7 # 假设的累计结果 elapsed = 42.5 # 假设的总用时(秒) minutes = elapsed / 60 wpm = (correct_chars / 5) / minutes if minutes > 0 else 0 accuracy = correct_chars / (correct_chars + wrong_chars) * 100 print(f'用时: {elapsed:.1f} 秒') print(f'速度: {wpm:.0f} WPM') print(f'正确率: {accuracy:.1f}%') # ----- 片段 6 (python) ----- import random import sys import time WORDS = [ 'python', 'function', 'variable', 'loop', 'dictionary', 'algorithm', 'compile', 'debug', 'module', 'recursion', 'terminal', 'keyboard', 'practice', 'speed', 'accuracy', 'language', 'console', 'integer', 'string', 'import', 'return', 'while', 'break', 'error', 'output', ] TOTAL = 10 def pick_words(): random.shuffle(WORDS) return WORDS[:TOTAL] def show_progress(done, total): bar = '#' * done + '-' * (total - done) sys.stdout.write(f'\r[{bar}] {done}/{total}') sys.stdout.flush() def compare(target, typed): correct = wrong = 0 for i in range(max(len(target), len(typed))): if i < len(target) and i < len(typed) and target[i] == typed[i]: correct += 1 else: wrong += 1 return correct, wrong def main(): targets = pick_words() total_chars = sum(len(w) for w in targets) correct_chars = wrong_chars = 0 start = time.time() for i, word in enumerate(targets, 1): show_progress(i - 1, TOTAL) print(f'\n第 {i}/{TOTAL} 个词:{word}') try: typed = input('输入: ').strip() except EOFError: break c, w = compare(word, typed) correct_chars += c wrong_chars += w elapsed = time.time() - start minutes = elapsed / 60 wpm = (correct_chars / 5) / minutes if minutes > 0 else 0 tried = correct_chars + wrong_chars accuracy = correct_chars * 100 / tried if tried else 0 print('\n' + '=' * 20) print(f'用时: {elapsed:.1f} 秒') print(f'正确字符: {correct_chars}/{total_chars}') print(f'速度: {wpm:.0f} WPM') print(f'正确率: {accuracy:.1f}%') if accuracy >= 95 and wpm >= 30: print('评价:非常棒!') elif accuracy < 80: print('评价:别急,先求准再求快') else: print('评价:不错,继续练习!') if __name__ == '__main__': main()