When normal people want to learn typing, they use softwares like Typing master. But since we are programmers, we can use our knowledge to write our own typing tutor app. Python, as always will be best for this purpose as it is easy to understand and provides a lot of libraries for our specific purpose. So lets begin!
In order to check our typing speed and accuracy, we need to record keystrokes. To do this, we will be using a python library known as tkinter. Since tkinter is already inbuilt from Python 3.4 and above, so you won’t need to install it using pip.
How this app works?
First of all, we will take a dataset of many english letters. Then we will randomly present words from this dataset to the user. The user has to type these words. If he types a word successfully, he will get the next word. Also, the user will get a specific number of lives. The user will lose a life as soon as he types a letter wrong. If his lives finish, the game gets over. Also, a score is calculated based on the time taken by user to type a particular word, and number of letters typed correctly.
The dataset of English words used in this project can be found here. Download this file and store it in the same location where the python file is present. This file contains about 10,000 words of English. We will be randomly selecting a given number of words from this list. For score calculation, we increment score by one each time a letter is correctly pressed by user. Also a time limit is assigned to each word, which is a multiple of the word length. If the user types the word before the time limit, the leftover time gets added to the score.
import tkinter as tk import random from os import system, name import time def clear(): if name == 'nt': _ = system('cls') else: _ = system('clear') index = 0 list_index = 0 list =  word_count = 10 f = open('words.txt', 'r') for line in f: list.append(line.strip()) random.shuffle(list) list = list[:word_count] print("---WELCOME TO TYPING TUTOR---") time.sleep(1) clear() print("Total words: ", len(list)) time.sleep(2) clear() print("Word "+str(list_index+1)+" out of "+str(word_count)+": "+list[list_index]) start_time = time.time() end_time = 0 time_multiplier = 2 lives = 3 score = 0 def keypress(event): global index global list_index global list global lives global score global start_time global end_time global time_multiplier word = list[list_index] if event.char == word[index]: index = index + 1 score = score + 1 else: clear() print("Word " + str(list_index + 1) + " out of " + str(word_count) + ": " + list[list_index]) print("wrong letter!") lives = lives - 1 print("Lives left: ", lives) if lives == 0: print("Game Over!") print("Final Score: ", score) root.destroy() return if index == len(word): clear() print("right!") index = 0 list_index = list_index + 1 end_time = time.time() time_taken = int(end_time - start_time) time_left = time_multiplier * len(word) - time_taken score = score + time_left print("time taken: " + str(time_taken) + " out of " + str(time_multiplier*len(word)) + " seconds.") print("Current score: ", score) time.sleep(1.5) start_time = end_time clear() if list_index < len(list) and index == 0: print("Word " + str(list_index + 1) + " out of " + str(word_count) + ": " + list[list_index]) elif list_index == len(list): clear() print("Congratulations! you have beaten the game!") print("Final Score: ", score) root.destroy() root = tk.Tk() root.bind_all('', keypress) root.withdraw() root.mainloop()
Copy this code to a new python file and name it as app.py. The total number of words to be displayed in the game can be set using
word_count variable. It is currently set at 100. The
time_multiplier variable controls the amount of time allotted to each word. Currently it is set at 2. This means for a word of length 5, the time limit is 5*2 = 10 seconds. The
lives variable defines the number of lives a player gets. A life gets over each time player incorrectly types a letter.
To run this code, open command prompt, change directory to the location where this python file is stored, and type:
Note that after you run this code, command prompt will not remain as the active window. You don’t have to worry about it. Just start typing the letters of the word displayed. As you keep typing correctly, the score will update and next words will continue to appear. Also you won’t physically see the words being typed anywhere on screen. You just have to keep pressing the correct keys. Lives will be deducted whenever any incorrect key is pressed.