Home > OS >  How to turn error message into a bool value?
How to turn error message into a bool value?

Time:02-24

i=5
while bool(controller[i]):
    controller_jsn['Transition']=controller[i]
    i =1

I write a code above. And try to stop the loop when i is out of range. bool() is not the way. what methode should I use?

CodePudding user response:

The more common way to write this code would be to iterate over the elements directly, instead of using indices. Use a slice if you need to start at the 5th element instead of looping over all elements.

for c in controller[5:]:
    controller_jsn['Transition'] = c

but I hope you have more logic in there, because each iteration of the loop just overwrites the same value.

  • Related