Create a typing test by Python

Lecturer: Julie Wang

Date: May. 10th, 2020

OUTLINE

  • Reason for choosing this topic
  • 檔案存取
  • 實作
  • Reference

Reason for choosing this topic

土豆曰:「練英打囉。」

WPM

Words per minute

  • 量度打字與速讀的速度
  • 每五個字視為一個單字

例如:

I run

視為一個字

Let's talk

視為兩個字

WPM 計算

Number_of_keystroke/time_in_minute * percentages_of_accurate_word
Number_of_keystroke/time_in_second * 60 * percentages_of_accurate_word/5 

計算時間

time.time()

回傳 1970 年至今經過的秒數

import time
print(time.time())

檔案存取

檔案存取順序

開啟檔案

存取檔案

關閉檔案

開啟檔案

f = open(file, mode)

檔案名稱

開啟檔案的模式

mode
'r' 讀取,預設模式
'w' 寫入,會覆蓋檔案內容
'a' 寫入,若已有內容則附加在末端
f = open(C:\\users\\user\\file.txt)
f = open('file.txt')

存取檔案

寫法
f.read(size) 依照 size 讀取內容
f.readline() 讀取一整列的資料
f.readlines() 讀取多列資料

Hello World!

This is a sample.

指標起始位置

f.read(7)位置

關閉檔案

f.close()

檔案存取順序

f.open(file, mode)

f.read()

f.close()

實作

LAB : Create a typing test

輸入欲練習的題數

紀錄開始時間

呼叫打字函式

讀檔,計算正確字數、題目長度

記錄結束時間

呼叫結算函式

總共花費時間、WPM、準確率

流程說明:

解說

import time
num = input("輸入題數:")
t1 = time.time()
correct, len_word = typingpractice(num)
t2 = time.time()
result()
def typingpractice(num):
    correct = 0
    len_word = 0
    f = open('A Lover\'s Complaint.txt', 'r')
    content = f.readlines()
    for i in range(int(num)):
        content[i] = content[i][:-1]
        print("-"*10)
        print("Try this:", content[i])
        print("長度:", len(content[i]))
    while True:
        ans = input("ans: ")
        if len(ans) != len(content[i]):
            print("輸入長度為", len(ans), ",字數不符重新輸入")
        else:
            for j in range(len(content[i])):
                if ans[j] == content[i][j]:
                    correct += 1
            len_word += len(content[i])
    	break
f.close()
return correct, len_word
def result():
    print("---結算---")
    print("時間(秒):", t2 - t1)
    print("WPM: {:.2f}".format(len_word/(t2 - t1)*60*correct/len_word/5))
    print("準確率: {:.2f}%".format(correct/len_word*100))
import time

def typingpractice(num):
    correct = 0
    len_word = 0
    f = open('A Lover\'s Complaint.txt', 'r')
    content = f.readlines()
    for i in range(int(num)):
        content[i] = content[i][:-1]
        print("-"*10)
        print("Try this:", content[i])
        print("長度:", len(content[i]))
        while True:
            ans = input("ans: ")
            if len(ans) != len(content[i]):
                print("輸入長度為", len(ans), ",字數不符重新輸入")
            else:
                for j in range(len(content[i])):
                    if ans[j] == content[i][j]:
                        correct += 1
                len_word += len(content[i])
                break
    f.close()
    return correct, len_word

def result():
    print("---結算---")
    print("時間(秒):", t2 - t1)
    print("WPM: {:.2f}".format(len_word/(t2 - t1)*60*correct/len_word/5))
    print("準確率: {:.2f}%".format(correct/len_word*100))

num = input("輸入題數:")
t1 = time.time()
correct, len_word = typingpractice(num)
t2 = time.time()
result()

完整程式碼

Reference

ShengYu Peng (2019) . [Python] 計算程式執行時間 . Retrieved from https://shengyu7697.github.io/blog/2019/03/14/Python-measure-execution-time/

uwenku (2014) .「WPM」如何在打字速度應用程序中計算? . Retrieved from http://hk.uwenku.com/question/p-fafexkzm-nt.html

Jeremy Hylton (2020) . The Complete Works of William Shakespeare . Retrieved from http://shakespeare.mit.edu/

Thanks for listening.

Made with Slides.com