-2

This code print value as:

['38']

['25']

['57']

['61']

['73']

etc... How do I make it return value as an integer?

Like this:

38

25

57

61

73
import BlynkLib

# Initialize Blynk
blynk = BlynkLib.Blynk('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

# Register Virtual Pins
@blynk.VIRTUAL_WRITE(1)
def my_write_handler(value):
    #print('Current V1 value: {}'.format(value))

    print(value)

while True:
    blynk.run()

Thanks

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

1 Answers1

0

When you execute

    print(value)

you see e.g.

['38']

You can pick out '38' by taking the zero-th element, using value[0].

Then, to convert it from string to integer, call int: int(value[0]).

If you might possibly get an empty list, then add a guard:

    if len(value) > 0:
        num = int(value[0])

Consider using tuple unpack:

    num, *_ = map(int, value)

Since you're apparently receiving a list of multiple values, it would be clearer to use the identifier values as the formal parameter of your handler callback function.

J_H
  • 17,926
  • 4
  • 24
  • 44