In this project you will implement a terminal-based typing speed game that calculates the player’s typing words per minute given some randomly generated phrase to type.
This project will challenge you to read data from at least one text file, break down a program into functions of your design, and learn a bit about how to work with the concept of time in a program.
Before you attempt to implement a non-trivial program, you should start with thinking through its design. This is often done best on paper or a whiteboard.
It’s often useful to first think through the user experience:
Once you have designed how you want the game interactions to go, then turn your attention toward what steps you need your program to complete in order to carry out each interaction. Some steps will be simple, some will be more complex. For the more complex interactions, try to break them down into a list of next steps. The process of turning these kinds of written steps into functions is an art that takes practice, but often you will find there is a close correspondence.
One thing you should look into and explore in a REPL is the time
module’s time
function whose documentation you can read here: https://docs.python.org/3/library/time.html#time.time. The short story is this function will return to you the amount of time, in seconds, that has passed since the beginning of time widely considered to be January 1st, 1970. Think through how you can use that knowledge to determine how long it took the user to give you an input string.
You will need to calculate words per minute based on the user’s input and the time it took (above). If this metric assumes a word is 5 characters long, on average, not including spaces, how will you calculate words per minute? Think through this algorithm and write out the algebraic expression you will use.
When it comes to data files with words, you are free to curate your own and search around. If you use a dictionary file from the internet, just include a link to its source in your program’s docstring. Here’s one with 1,000 common English words, for example. If you’d like to produce phrases that read closer to real sentences, feel free to make use of multiple dictionary files for different kinds of words. Also consider producing a dictionary file of words (or symbols!) meaningful to you in your field. For example, if you wanted to practice typing for computer science, including some symbols in your game such as [
, ]
, (
, )
, ~
, /
, :
, while
, and so on, could be helpful typing practice.
When choosing a random word from the dictionary file(s) you read in, read through the random
module’s functions that are made for working with sequences like Lists: https://docs.python.org/3/library/random.html#functions-for-sequences
Since this project will have not only your Python code, but dictionary file(s) you will submit, as well, lets setup a new directory for it.
comp110
directory, add a directory named typing_speed
.
comp110
and a sibling to your previous projects
directory.typing_speed
directory, add two files named game.py
and game_test.py
__author__
global variable.main
function idiom and should call main
if __name__
is equal to __main__
. Other than global/named constants, all statements should be in function bodies.typing_speed
directory, add a file named words.txt
open
it is "comp110/typing_speed/words.txt"
main
10pts - Read a dictionary of words from a plain-text file. Reading more than one file is OK. Must include any such dictionary files in the same directory as the project.
10pts - Generate random phrases whose words are at least partially sourced from words in dictionary files.
10pts - Correctly calculate the user’s typed words per minute based on a word being considered 5-characters long on average, not including spaces. Words per minute reported should be rounded to the nearest int
(see Python’s built-in round
function).
10pts - Write some tests for at least one of your program’s pure functions in game_test.py
. A pure function is one that does not involve side-effects. At least one task is prime for being broken out into its own pure functions and tested: calculating words per minute given a string and the number of seconds it took to type it. Many other functions in this program are likely to involve side-effects, including those that print, generate random numbers, promp for input, or involve input/output. Testing functions which involve side-effects is more challenging and beyond our current concerns.
10pts - User should be able to continue attempting different phrases, and be told their words per minute after each attempt. User should also be given an obvious way to quit. The user experience of using your typing speed program should be pleasant.
15pts - Program should meet linted, style expectations.
15pts - Program should make full use of static type hints in function declarations.
5pts - Go above and beyond with one or more of the extensions below.
You are encouraged to attempt as many as these as you’re feeling up to the challenge of. For any you attempt, be sure to document in your
random
module’s seed
function in your test cases to make the “random” functions always return the same sequence of random numbers.time
module’s sleep function: https://docs.python.org/3/library/time.html#time.sleep