While Loops

While loops, more commonly known within National 5 as ‘Conditional Loops’ run until a condition has been met, or while a condition is being met.

For instance, while a number is greater than 0, we can keep repeating the code. As soon as the number is less than 0, the code exits the loop and continues.

number = 100 # Sets number to 100
while number > 0: # While loop repeats while number is greater than 0
    print("The number is", number) # Printing out the number
    number_to_remove = int(input("Enter a number between 1 and 10") # Ask for another number between 1 and 10
    number = number - number_to_remove # reduce number by the new number
    # At this point, if number is less than or equal to 0, the code stops repeating

print("Finished the program!") # This runs outside of the conditional loop

Target

You should be able to use the Conditional Loops to solve a problem.