Iterators and generators · 6 / 15 Exercise Easy

Looping with iter and next

Objective

Create a Python program that goes through a list by hand, the way the for loop does internally.

Instructions

  • Read a line with words separated by spaces (at least one).
  • Get an iterator with iter(words) and ask for the elements with next() inside a while True. Catch the StopIteration exception with try/except to leave the loop.
  • Show Item: X for each word and, when finished, End.
Input
sol luna mar
Output
Item: sol
Item: luna
Item: mar
End

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise