EasyManua.ls Logo

SunFounder ESP32 - Page 296

Default Icon
771 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder ESP32 Starter Kit
numbers = [1, 2, 3, 4]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)
>>> %Run -c $EDITOR_CONTENT
The sum is 10
The break Statement
With the break statement we can stop the loop before it has looped through all the items:
numbers = [1, 2, 3, 4]
sum = 0
for val in numbers:
sum = sum+val
if sum == 6:
break
print("The sum is", sum)
>>> %Run -c $EDITOR_CONTENT
The sum is 6
290 Chapter 3. For MicroPython User

Table of Contents