Python - Time

There is an extra set of commands that can be added to Python that allow you to tell the time.
We access the commands by importing the time library.

import time as clock

The time library measures time in seconds since midnight on 1st January 1970.|

The program below uses .time() to get the number of seconds since 1/1/70

Watch the video to see how to use time commands in Python

Python - Time.mov

StopWatch

By recording the time at two different intervals and storing the values in variables, we can work out how long something takes.

Watch the video to see how to make a simple stopwatch program

Stop Watch Demo

PythonStopWatch.mov

Estimate 10 seconds

Create a Program that checks to see how good you are at estimating 10 seconds...

  • You will need to store the time the user starts in a variable

  • You will need to store the time the user finishes in a different variable.

  • You will need to calculate the difference between the two times

  • The program should tell you if you estimated 10 seconds correctly.

Hint

  • Ask the user to press enter when they are ready to start

  • Use input() to tell when they have pressed enter

  • Store the current time in a variable

  • Tell them to wait 10 seconds and press enter.

  • Use input() to tell when they have pressed enter

  • Store the current time in a different variable

  • in another variable store the result of subtracting the first time from the last as an integer

  • Output how long it took them

  • IF the time they guessed == 10, then tell them they guessed correctly

  • ELSE tell them that they guessed incorrectly.

How fast can you type the alphabet?

Create a program that times how fast you can type the alphabet.

  • You will need to store the time the user starts in a variable

  • You will need to store the time the user finishes in a different variable.

  • You will need to calculate the difference between the two times

Hint

  • Ask the user to press enter when they are ready to start

  • Use input() to tell when they have pressed enter

  • Tell them to type in the alphabet and press enter.

  • Store what they type in a variable.

  • Store the current time in a variable

  • In another variable store the result of subtracting the first time from the last.

  • Output how long it took them

Extension:

Create a while loop that checks they have typed in the alphabet correctly. It should tell them they have made a mistake and to type the alphabet again.

More Resources

For more information about the time in Python 3 check out: