0

I have 5 sensors:

the sensors read the environment simultaneously

what I am trying to do is increment the sensor number with each scan

for i in range(0,5):
self.sensor_i.append(Quantity("HC-SR04.{n}".format(n=i), Quantity.FLOAT))

what I am trying to do is, each iteration I want to increase the sensor number and sensor scaned data as follow sensor_1 = sensor data at time 1 sensor_2 = sensor data at time 2 .... etc

How can I increment it without any error.

MEKH
  • 39
  • 1
  • 8

1 Answers1

0

Use the keyword eval

for i in range(0,5):
    eval("self.sensor_" + str(i)).append(Quantity("HC-SR04.{n}".format(n=i), Quantity.FLOAT))

Referencing: Converting string to preexisting variable names

Uchiha012
  • 821
  • 5
  • 9