design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

6.5a

LO: Bringing it all together.

We have a task: create a program that will work out if a number is prime or not (a number that is divisible only by itself and 1). This program will have to satisfy the following:

Let's look at these requirements in more detail, links to the relevant exercises will be given for reference.

It is important that when writing a program as complex as this we do not simply just start writing and complete the program from start to finish. Remember that the shell in IDLE is there so you can try out your code and you can write small programs that just complete a part of the completed program - once they have been tested and you know that they work they can be written into the final program.*



Let's start with how to calculate if a number is prime. To check if a number is prime we have to see if that number is divisable by any number starting from two all the way up to one less than the number e.g. to check if 16 is prime, divide 16 by 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and 15. This looks like a range of numbers starting at 2 and ending at number - 1:


#first we need a function that accepts a number
def check_if_prime(number):

#create a range
range(2, number)

#check if the numbers is divisable by the numbers in the sequence
#if the number is divisable then it will not have a remainder
if (number % i == 0) 

#let's put this together
def check_if_prime(number):
    for i in range(2, number):
        if (number % i == 0):
            print(i, "can divide by") #make the output readable
    print("")

#call the function
check_if_prime(20)

			

If we run this as a small program it will print out a list of what numbers 20 can be divided by. If we change the number to 17 in the function call then we can see that it can't be divided by any of the numbers, nothing is printed - it is prime. This program is great for testing, we have used the print function to allow us to see what is going on and check if it correct. Here we need to remember one of our recomendations for writing programs from 4.1b Hide the algorithim from the user - the user does not need to know how the program is working, they do not need to see anything apart from the output (the answer they want). The user does not needd to see all of this output so we can now change the function now that we have tested it:


def check_if_prime(number):
    for i in range(2, number):
        if (number % i == 0):
            return False
        
    return True #if no False has been returned it must be True

			

Now we are able to send a number to the function and have it return True or False (that we are able to evaluate with conditional statements). So let's deal with that now using an if statement:


if check_if_prime(number):
    print(number, "is a prime number")
else:
    print(number, "is not a prime number")

			

Remember that the if statement will evaluate if something is True. We have used the function call check_if_prime(number) here because it returns either True or False!**


We have to get the user input and cast it to an integer:


user_input("Enter a number you wish to check: ")

#cast to int
number = int(user_input)

			

Make sure the number is not too big:


if number < 500:
        #we can put our if statement in here later
    else:
        print("Please enter a number smaller than 500")

			

Have a go at putting this together inside a game loop with the introduction text before looking at the code below - think about what order you would need to write out the different instructions:







#declare a flag variable
play = True

#start the game loop
while play:

    #define the function first
    def check_if_prime(number):
        for i in range(2, number):
            if (number % i == 0):
                return False
        return True

    #introduction and instructions
    print("  ***** AMAZING PRIME NUMBER CHECKER *****  ")
    print("") #space for clarity
    print("Welcome to the amazing prime number checker!")
    print("This program will check if a number, below 500, is a prime number")
    print("") #space for clarity

    #get the input from the user
    user_input = input("Enter a number you wish to check: ")
    #cast the input
    number = int(user_input)

    #check if the number is less than 500
    if number < 500:
        #it is, perform check
        if check_if_prime(number):
            print(number, "is prime")
        else:
            print(number, "is not prime")
    else:
        print("Please enter a number smaller than 500")

    print("Do you wish to test another number?")
    #give user option to quit the program
    user_input2 = input("Type q to quit or any other key to continue: ")

    if user_input2 == 'q':
        break #breaks the game loop

print("")  #space for clarity
print("Thanks for using the amazing prime number checker")

			




* I have said 'written' as cutting and pasting can lead to errors in indentation and is often a shortcut for thinking.


** You see now why we have been including return statements in our functions - they are very powerful.


Code Examples

Resources