I have the following helper function that works out the total steps from a multple list of readings
eg ['40', '1571', '1366', '691', '947', '1947', '108', '132', '950', '1884']
def data_check(readings, total):
for reading in readings:
steps = int(reading)
total = total + steps
return total
I need to alter the program so it can deal with bad data, such as strings or negative values, and adds zero to the total when a bad value occurs.
e.g ['40', '1571', '13vgs6', '-5', '947']
my solution was as follows, which doesnt work the total out properly gives me a division by zero error later in the program:
def data_check(readings, total):
""" checks to see if data is good format and returns total """
print(readings)
for reading in readings:
if type(reading) == int:
steps = int(reading)
else:
steps = 0
total = total + steps
return total
perhaps i am using a bad approach and shoudl maybe have a helper function that replaces all bad values in the list with zeros before hand?