I performed measurements with two independent sensors. Due to some whatever reason, one of the sensors became bad during testing, and I would like to make a new list containing the five first elements of sensor1
and the remaining elements from sensor2
. I managed to do this simply by:
updated = []
index = 0
while index < len(sensor1):
if index <= 2:
updated.append(sensor1[index])
elif index > 2:
updated.append(sensor2[index])
else:
print('You did something wrong, you ignorant fool!')
index += 1
However, in order to get more accustomed to Python, I would like to translate this to a function named Updater
def Updater(one, two, divide):
updated = []
index = 0
while index < len(one):
if index <= divide:
updated.append(one[index])
elif index > divide:
updated.append(two[index])
else:
print('You did something stupid, you ignorant fool!')
index += 1
return updated
which I call by
data = Updater(one=sensor1, two=sensor2, divide=4)
or
data = [Updater(a, b, divide=4) for a, b in zip(sensor1, sensor2)]
Alas, Updater
does not work, as it only performs the first iteration, so the index
is equal to 1
, although it should be equal to 13
, which is the length of the sensor1
and sensor2
.
- What am I doing wrong?
- How can I make this specific piece of code work within a function?
return updated
is indented once too many times.