LESSON 06 –
WHILE LOOPS

 

 

LESSON NOTE/GUIDE

PROGRAM 1 – OUTPUT NUMBERS 1 TO 10

·       Create a program that will use a loop to output numbers from 1 to 10.  While this would be a perfect scenario to use a for loop, make use of a while loop instead.


THE CODE

 

val = 1

 

while val <= 10:

    print(val)

    val=val+1


EXPLANATION

 

We can use a while loop for any kind of loop.  Usually, while loops are used when we do not know the number of times that we need to loop in advance.  When we do know how many times we will loop, we generally use a for loop instead.

 

The first line of code creates a variable val and sets it to one.

 

The next line creates a while loop.  It will loop as long as the variable val has a value that is smaller or equal to 10.

 

The final two statements are indented and therefore in the loop.  Each time they are executed, they print the value of val and then increase val by one.

 

After looping 10 times, val will get the value of 11 and the loop will no longer run.

 

 

PROGRAM 1B – INFINITE LOOP

·       Consider the previous program.  What happens if we comment out the last line?


THE CODE

 

val = 1

 

while val <= 10:

    print(val)

    # val=val+1


EXPLANATION

 

This results in an infinite loop because the value of val doesn’t change and hence the condition val <= 10 will always be true.

 

 

PROGRAM 2 – OUTPUT A MESSAGE 10 TIMES

  • Use a while loop to output a message to screen 10 times.


THE CODE

 

val = 1

 

while val <= 10:

    print("hi")

    val=val+1

 

EXPLANATION

 

Again, this application would usually be solved with a for loop.  But for learning purposes, we are using a while loop.

 

Notice that this code is identical to the code in Program 1 with the exception of the third line.  Instead of printing the value of val, we simply output the desired message.

 


PROGRAM 4 – USER CONTROLLED LOOP

  • Use a while loop that continuously loops as long as the user wants it to.

 


THE CODE

 

done=False

while done==False:

    ans = input("Keep going? (y/n)")

    if ans=="n":

        done=True

 

EXPLANATION

 

Note that understanding this code is much easier if you run it before reading the explanation.

 

The first line of code creates a variable done and sets it to False.

 

The next line can conveniently be read as “while done equal to false”.  It creates a loop that includes all three statements under it.

 

The first line in the loops displays “Keep going? (y/n)” to the user and waits for a response to be entered. 

 

The second line in the loop checks if the response is “n”.  If it is, then the variable done is set to True which ends to the loop.  If the response is anything but “n”, then done doesn’t get changed and we keep on looping.

 

 

PROGRAM 4B – USER CONTROLLED WITH TOTAL

  • Write a program that gets a number from the user and then asks the user if he/she wants to enter another number.  Once the user is done entering numbers, the total of all entered numbers is outputted.

 


THE CODE

 

total=0

done = False

while (done == False):

    n = input("Enter a number")

    total = total + int(n)

    ans = input("More? (n/y)")

    if ans == "n":

        done = True

print("The total is", total)

 

EXPLANATION

 

Note:  The text in black is taken directly from the previous program.

 

This code adapts the previous program to ask the user for a number and then if the user wants to continue.  This goes one as long as the user wishes.  Once the user chooses to stop, the total of all numbers is outputted.

 

 

PROGRAM 5 – ESCAPE VALUE

  • Use a while loop that asks a user to enter a positive number.  It provides a way to stop the loop by using a negative number.  After the loop ends, the total of all positive numbers entered is displayed.

 


THE CODE

 

total=0

done=False

 

while done == False:

    ans = int(input("Enter a positive number (-1 to stop)"))

    if ans >= 0:

        total = total + ans

    else:

        done = True

print("The total is", total)

 

EXPLANATION

 

This program again asks the user to input numbers continuously.  Instead of having to also ask if the user wants to continue every time, it has a convenient escape value that ends the looping. 

 

Of course, such a solution is only possible if there is an escape value.  For example, if -1 was a legal value in the data that the user was inputting, this solution would not work.