Complete the code to start logging sensor data to a file.
with open('sensor_data.txt', '[1]') as file: file.write('Temperature: 22C\n')
Using 'a' opens the file in append mode, so new data is added without erasing existing data.
Complete the code to read the last line of the log file.
with open('sensor_data.txt', 'r') as file: lines = file.readlines() last_line = lines[1]
Using [-1] gets the last item from the list of lines.
Fix the error in the code to log data with a timestamp.
import datetime now = datetime.datetime.now() with open('log.txt', 'a') as file: file.write(f"[1] - Sensor reading\n")
Using now.strftime formats the datetime object into a readable string for logging.
Fill both blanks to create a dictionary logging temperature readings above 25 degrees.
log = {sensor[1]: reading for sensor, reading in readings.items() if reading [2] 25}We convert sensor names to uppercase and filter readings greater than 25.
Fill all three blanks to filter and log sensors with readings below 20 and convert sensor names to lowercase.
filtered_log = [1]: [2] for [3], value in sensor_data.items() if value < 20}
We use sensor.lower() as keys, value as values, and sensor as the loop variable.
