3. Executing Python – Programs, Algorithms, Functions
Activity 8: What's the Time?
Solution:
In the following, you will find the solution code to Activity 8, What's the Time?
To make it easier to understand, the code has been broken down with explanations:
current_time.py
:
""" This script returns the current system time. """
- Firstly, we import the
datetime
library, which contains a range of useful utilities for working with dates:import datetime
- Using the
datetime
library, we can get the currentdatetime
stamp, and then call thetime()
function in order to retrieve the time:time = datetime.datetime.now().time()
- If the script is being executed, this
if
statement will be true, and, therefore, the time will be printed:if __name__ == '__main__': print(time)
You should get the following output:
At the...