Python for Kids: Python Typing Test:

Table Of Content:

Introduction: 

In this article, we are going to be using Python to create a simple typing test program. This program will ask users to type a sentence and will let them know how many errors they made and how fast they typed the phrase.

Python Typing Test

This project is a great way to learn more about making functional programs by creating a functional and versatile program. It’s a great way to build both your creative thinking and problem-solving skills while making a useful and practical project.

Who is this Project For?

This is an intermediate-level project for those who are new to Python, or those who want a fun way to apply their coding skills. Before starting this project, you should have some experience with creating and using functions, loops, and conditional statements such as if-statements. These concepts are widely used in any computer program, and your knowledge of these concepts will be developed as you work through this project.

What Will We Learn?

This program focuses on creating a functional reminder program to deliver notifications to users’ desktops. This teaches concepts such as if-statements, functions, and using operators as well as other essential programming concepts.

Features to Consider:

  • The user will be given a phrase to type
  • They will press enter to begin the timer, type their sentence, and press enter to end the timer
  • The program will check how many errors the user made and will present them with their time and accuracy results.

Pseudo Code:

The pseudocode for this project is included below:

Import the time library
Function typingErrors (prompt)
	Create a variable called iwords
	Set errors variable to 0
	For each letter or word in the sentence:
		If the letter or word is incorrect:
			Increase errors by 1
		Else:
			Continue
Function typingSpeed (iprompt, stime, etime)
	Create global variables time and iwords
	Split the words
	Set twords to the length of iwords
	Speed = twords/time
Return speed
Function timeElapsed (stime, etime)
	Time = etime/stime
	Return time
Create the text prompt
Ask the user to press enter and enter the text when they are ready
Set stime,and etime to time()
time = round(timeElapsed(stime, etime), 2)
speed = typingSpeed(iprompt, stime, etime)
errors = typingErrors(prompt)
print("Total Time elapsed : ", time, "s")
print("Your Average Typing Speed was : ", speed, "words / minute")
print("With a total of : ", errors, "errors")

Python Typing Test

Main Steps:

This project can be broken down into 3 main steps: 

  1. Create the typingErrors function
  2. Create the typingSpeed and timeElapsed functions
  3. Create the program’s input and output

Step 1: Create the typing Errors Function

The first thing we’ll need to do when creating this program is to import the time library. This will allow the program to time how long the user takes to enter the sentence in the program and will let them know how fast they can type. Then, we’ll need to create our typingErrors function to check how many errors the user makes while typing.

from time import time
def typingErrors(prompt):
    global iwords
    words = prompt.split()
    errors = 0
    for i in range(len(iwords)):
        if i in (0, len(iwords)-1):
            if iwords[i] == words[i]:
                continue
            else:
                errors +=1
        else:
            if iwords[i] == words[i]:
                if (iwords[i+1] == words[i+1]) & (iwords[i-1] == words[i-1]):
                    continue
                else:
                    errors += 1
            else:
                errors += 1
    return errors

As you can see, this function breaks the long sentence down into smaller words and checks whether the user has typed them correctly. If not, their error score is incremented by 1. 

Python Typing Test

Step 2: Create the typingSpeed and timeElapsed Functions

Next, we need to create the typingSpeed function. This will be used to calculate how many words the user types per minute. The code for this function is as follows:

def typingSpeed(iprompt, stime, etime):
    global time
    global iwords
    iwords = iprompt.split()
    twords = len(iwords)
    speed = twords / (time/60)
    return speed

Now it’s time to create the timeElapsed function to check how long it takes for the user to type their sentence. To complete this step, we will type:

Now its time to create the timeElapsed function to check 
how long it takes for the user to type their sentence.
 To complete this step, we will type:
<code>
def timeElapsed(stime, etime):
    time = etime - stime
    return time

As you can see, we now have two functions that can be used to calculate the user’s score by telling them their time and how many words they can type per minute.

Python Typing Test

Step 3: Create the Program’s Input and Output

Finally, we need to create the program's input and output and set up some of our variables. We will begin by creating a prompt that the user needs to type in, and then printing it and asking the user to press the enter key to begin the timer. Then, we will use the time library to select the correct values for stime, etime, time, and speed. Finally, we will print the user’s results and the program is done! This is the final section of code for this project, and it is as follows:

if __name__ == '__main__':
    prompt = "the quick black fox jumped over the lazy dogs"
    print("Type this: '",prompt, "'")
    input("press ENTER when you are ready!")

    stime = time()
    iprompt = input()
    etime = time()
    time = round(timeElapsed(stime, etime), 2)
    speed = typingSpeed(iprompt, stime, etime)
    errors = typingErrors(prompt)

    print("Total Time elapsed : ", time, "s")
    print("Your Average Typing Speed was: ", speed, "words/minute")
    print("With a total of : ", errors, "errors")

Project Complete!

Python Typing Test

Now the project is complete! We hope that you’ve enjoyed creating this simple typing test. Feel free to test your code out and check the code mentioned above as a reference if you have any errors.

Geek Team

Geekedu is an expert in Coding and Math learning. Our goal is to inspire and empower youth to use their knowledge of technology to become the influencers, inventors and innovators of the future.

Sign up and get a 60-minute free assessment class

Book A FREE Trial