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:

The program will introduce itself and provide instructions for the user.

The program will accept user input and allow the user to quit the program.

The program will impose a limit on the number the user can enter.

The answer will be given to the user in a readable way.

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

The program will introduce itself and provide instructions for the user: This was covered from 4.1b which also provided some other guidelines to writing programs. This part of the program could be written seperatly and then written into the final program, when it works.

The program will accept user input and allow the user to quit the program: User input and casting (remember we will be dealing with numbers) were covered from 4.2b, allowing the user to quit was included in the odd or even program covered in 6.4b that can be found in the code examples below.

The program will impose a limit on the number the user can enter: As we will discuss later, there is no quick way to calculate prime numbers so this task can take a long time. If it is too long then the program may not be able to solve it in a reasonable time or simply crash. We have looked at assessing the size of numbers when we made our guessing game in 5.2b.

The answer will be given to the user in a readable way: Instead of printing out 'Yes', our program will include the user input in the answer '7 is a prime number' or '10 is not a prime number'.

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 numberdef check_if_prime(number):#create a rangerange(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 remainderif (number % i == 0)#let's put this togetherdef check_if_prime(number): for i in range(2, number): if (number % i == 0): print(i, "can divide by")#make the output readableprint("")#call the functioncheck_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 intnumber = int(user_input)

Make sure the number is not too big:

if number < 500:#we can put our if statement in here laterelse: 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 variableplay = True#start the game loopwhile play:#define the function firstdef check_if_prime(number): for i in range(2, number): if (number % i == 0): return False return True#introduction and instructionsprint(" ***** AMAZING PRIME NUMBER CHECKER ***** ") print("")#space for clarityprint("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 useruser_input = input("Enter a number you wish to check: ")#cast the inputnumber = int(user_input)#check if the number is less than 500if number < 500:#it is, perform checkif 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 programuser_input2 = input("Type q to quit or any other key to continue: ") if user_input2 == 'q': break#breaks the game loopprint("")#space for clarityprint("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.