Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON TEXT / CHAPTER 03 / Learn / LESSON 08
Eighth class. A loop often counts or searches instead of only printing.
A counter starts at 0. On each match you increase it. A search uses a flag (for example found = False) and break on the first hit.
Mini example:
text = "abca" count = 0 for ch in text: if ch == "a": count = count + 1 found = False for ch in text: if ch == "c": found = True break print(count) print(found)
Common mistake: resetting the counter inside the loop; or forgetting break and searching to the end unnecessarily. We do not use the built-in string count() yet — we deliberately practice the loop.
Learn: accumulation and early stop when searching.
AWAITING TYPES
›