The following exercises cover scenarios on While loop statement in Python.
Exercise 1
Print ‘Hello World’ 5 times using While loop.
i = 0 i < 5 print('Hello World')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 2
Iterate over the items in the list mylist
.
mylist = ['apple', 'banana', 'cherry'] i = 0 i < len(): print(mylist[i]) i += 1
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 3
Iterate from i=1
to i=10
, but break the loop if i
equals 4.
i = 1 while i <= 10: if i == 4: print(i) i += 1
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 4
Iterate fromi=1
to i=10
, but if i
equals 4, skip the next statements in the loop and continue with the next iteration.
i = 1 while i <= 10: if i == 4: print(i) i += 1
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 5
Print 'Hello World'
when the condition in While loop becomes false, using while-else.
i = 1 while i <= 10: print(i) i += 1 print('Hello World')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.