Skip to content

A3 - The while loop

Consider these two blocks of code:

### Block 1
guess = input("What is the password? ")
if guess != "my_password":
    guess = input("Incorrect - try again: ")

### Block 2
guess = input("What is the password? ")
while guess != "my_password":
    guess = input("Incorrect - try again: ")

The two blocks only differ by a single word: if vs while.

  • When we use if, it will check once if the password is wrong, then keep going.
  • When we use while, it will repeatedly check if the password is wrong, and only continue once it's correct.

A good use of while is for data validation: ensuring that something the user entered in is valid. Consider this example:

response = input("Enter a number between 1 and 10: ")

while not response.isdigit() or int(response) < 1 or int(response) > 10:
    response = input("Invalid Input. Enter a number between 1 and 10: ")

print(f"Your response of {response} is valid")

The while condition has three parts to it, separated by or:

  • not response.isdigit() checks if they entered in something that is not a digit
  • int(response) < 1 checks if they entered in a number that's too low
  • int(response) > 10 check if they entered in a number that's too high

If any of the above are true, the loop will continue to iterate. It is impossible to get past the loop until a number between 1 and 10 has been entered!

Finally, you could use a while loop to do something like count:

num = 1
while num <= 20:
    print(num)
    num += 1

We could add an if statement to the mix so that it prints a * in place of any multiple of 3:

num = 1
while num <= 20:
    if num % 3 == 0:
        print("*")
    else:
        print(num)
    num += 1

Your Task

Part A

In a file called a3a.py, write a program similar to this example from above:

response = input("Enter a number between 1 and 10: ")

while not response.isdigit() or int(response) < 1 or int(response) > 10:
    response = input("Invalid Input. Enter a number between 1 and 10: ")

print(f"Your response of {response} is valid")

Instead of asking for a number between 1 and 10, however, change the condition. Consider something that isn't trivial - for example, you could ask for an even number greater than 100.

Part B

In a file called a3b.py, write a similar program to the one from Part A, but this time you should be expecting a string as a response with some conditions. For example, you could ask for something that starts with the letter V, or is less than 10 letters, or is all uppercase. You could even ask for multiple conditions.

For the while condition, you'll need to make use of the string methods you learned in the previous assignment.

Part C

In a file called a3c.py, write a modified version of the counting example:

num = 1
while num <= 20:
    if num % 3 == 0:
        print("*")
    else:
        print(num)
    num += 1

Ideas you could try - counting by fives, counting down instead of up, modifying the condition for replacing a number with a symbol. Try to get creative!