-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor_data_analyzer.py
More file actions
59 lines (47 loc) · 1.57 KB
/
Copy pathsensor_data_analyzer.py
File metadata and controls
59 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
readings = []
try:
#read the sensor dat
with open("sensor_data.txt", "r") as file:
for line in file:
line = line.strip()
if line == "":
continue
reading = float(line)
readings.append(reading)
# Make sure we actually have data
if len(readings) == 0:
print("Error: the file contains no sensor readings.")
else:
# Ask the user for a threshold
threshold = float(input("Enter threshold: "))
#Calculate statistics
minimum = min(readings)
maximum = max(readings)
average = sum(readings)/len(readings)
above_threshold = [
reading for reading in readings
if reading > threshold
]
#store the results in a dictionary
statistics = {
"minimum": minimum,
"maximum": maximum,
"average": average,
"above_threshold_count": len(above_threshold)
}
#display results
print("\n--- Sensor Analysis ---")
print("Number of readings:", len(readings))
print("Minimum:", statistics["minimum"])
print("Maximum:", statistics["maximum"])
print("Average:", statistics["average"])
print("\nReadings above threshold:")
print(above_threshold)
print(
"Number above threshold:",
statistics["above_threshold_count"]
)
except FileNotFoundError:
print("Error: sensor_data.txt was not found.")
except ValueError:
print("Error: Invalid Numeric data found.")