Time Guessing Game

stopwatch-micro-bit

Reaction Time Tester


Look at the code provided below. It uses the time library to store the current time (as a number of seconds) at different lines of the code.

The code is executed very quickly, normally in just a few milliseconds, one line at a time. This is called sequencing. However from time to time your code is paused, waiting for the user to input a value. This is the case in Python every time you use the input instruction.

By recording the current time before and after an input instruction, we can then calculate how long it took for the user to type an answer. This is the approach used by the following Python program used to calculate your reaction time (how quickly will you press the return key).


Time Guessing Game


Adapt this script to:

  1. Generate a random number between 1 and 10,
  2. Display the number on screen and ask the user to wait for this number of seconds before pressing the return key,
  3. Calculate how long they waited for and give the user a score as follows:
    • If the user waited for more than the given time, they went bust: 0 points,
    • If the user press the return key at the right time or up to 1/2 second earlier: 50pts,
    • If the user press the return key between 1 second to 1/2 second earlier: 25pts,
    • If the user press the return key between 2 seconds to 1 second earlier: 10pts,
    • If the user press the return key more than 2 seconds earlier: 0pts.
    Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges Tagged with:

Our Latest Book