Let’s check out some exercises that will help understand While Loops better.

Exercise 9-a

Write a while loop that adds all the numbers up to 100 (inclusive).


you can start with while i < 100:

One of the key aspect of writing while loops is watching your counters. For instance, when you type while i < 100:

If you don’t increase in every loop it will create an infinite loop as i will stay the same and always below 100. Eventually you’ll get a runtime error.

So defining i before the loop:
i = 0

And increasing i inside the loop:
i = i+1

are key points in most while loops.

This can be confusing for absolutely new programmers but you also need another counter which adds up the numbers during the iteration.

With some repetition and pondering you will definitely get a grip of while loops, it’s normal to struggle with them slightly more than for loops which usually doesn’t have to bother with counters for the loop to function properly.

So,
c = 0
before the loop;

and,
c = c+i
inside the loop will ensure that c adds each number to the sum along the way during the iteration.

i = 0
c = 0
while i < 100:
    c= c+i    
    i= i+1
print(c)

Exercise 9-b

Using while loop, if statement and str() function; iterate through the list and if there is a 100, print it with its index number. i.e.: "There is a 100 at index no: 5"


You can start with defining a counter:
i = 0

To make sure you go through the list you can construct your while loop as:
while i < len(lst)

Most important thing to remember, so that you don’t get an error is to increase i in each iteration as:
i = i+1

Inside your while loop, you can use an if statement as such:

if lst[i] == 100:
    …

Besides being the counter i is being used as the index throughout the list and lst[i]  is the value of elements in the list at index: i

i = 0
while i < len(lst):
    if lst[i] == 100:
        print("There is a 100 at index no: " + str(i))
    i = i+1

Exercise 9-c

Using while loop and an if statement write a function named name_adder which appends all the elements in a list to a new list unless the element is an empty string: "".


You can start with defining a counter and a new empty list which will be used for appending.:
i = 0
new_list = []

and inside the loop i = i+1

Again you can iterate during the length of the list.

Inside the loop you can make an if statement that

if list[i] != “”:
    …

which means if list[i] is not equal an empty string.

Make sure your function returns the new list too.

def name_adder(list):
    i = 0
    new_list = []
    while i < len(list):
        if list[i] != "":
            new_list.append(list[i])
        i = i+1
    return new_list

Exercise 9-d

This time inside a function named name_adder, write a while loop that stops appending items to the new list as soon as it encounters an empty string: "". And prints "There is an empty string and returns the new list."


You can use an if-else statement and a break statement inside the while loop.

You can start with defining a counter and a new empty list which will be used for appending.:
i = 0
new_list = []

and inside the loop i = i+1

if list[i] != "":
new_list.append(list[i])
else:
break

 

def name_adder(list):
    i = 0
    new_list = []
    while i < len(list):
        if list[i] != "":
            new_list.append(list[i])
        else:
            break
        i = i+1
    return new_list
print(name_adder(lst1))