I'm not sure what I'm doing wrong here, but for some reason, every time I correct the code or try to get it to iterate through the CSV file, it gives me a float object not iterable. If someone could give me some pointers on how to iterate through the array of floats, I would be grateful.
import pickle
from weatherreading import WeatherReading
def valid_temperature(temperature : float):
return -40 < temperature < 85
def valid_pressure(pressure : float):
return 300 < pressure < 1200
def valid_humidity(humidity : float):
return 20 < humidity < 80
def valid_reading(temperature : float, pressure : float, humidity : float):
return valid_temperature(temperature) and valid_pressure(pressure) and valid_humidity(humidity)
def get_readings_from_file(filename : str, skip_first_line : bool) -> list[WeatherReading]:
with open(filename, "r") as file:
# Note that we're stating that the list is a List of WeatherReading instances
all_readings : list[WeatherReading] = []
first_line = True
for line in file:
if skip_first_line and first_line:
first_line = False
continue
split = line.split(",") #split the data at every comma
temperature = float(split[0]) #First column
pressure = float(split[1]) #second column
humidity = float(split[2]) #third column
def max_temperature():
maxTemp = []
for i in temperature:
maxTemp.append(i)
return max(maxTemp)
def min_pressure():
minPress = []
for i in pressure:
minPress.append(float[i])
return min(minPress)
def max_humidity():
maxHum = []
for i in humidity:
maxHum.append(float[i])
return max(maxHum)
if valid_reading(max_temperature(), min_pressure(), max_humidity()):
reading = WeatherReading(max_temperature(), min_pressure(), max_humidity())
all_readings.append(reading)
return all_readings
if __name__ == "__main__":
filename = input("Enter CSV file name: ")
all_readings = get_readings_from_file(filename, True)
# Echo the input to output.
print("Your readings:")
for reading in all_readings:
print(f"\tTemp: {reading.temperature}", end="")
print(f"\tPressure: {reading.pressure}", end="")
print(f"\tHumidity: {reading.humidity}")
pickle_demo = open('pickle_demo.bin', 'wb')
pickle.dump(all_readings, pickle_demo)
pickle_demo.close()
max_temperatureandmin_pressurefunctions you are trying to loop over floats which are not iterable.for i in temperature<-- temperature is a float. You can't do a for loop over a floatmax, right?