Python scripting
Guided Practice 1.4 – Design, write, test and correct
Task 1 – Designing a program
When writing a program, it is important to design all parts of the program so it will do what you want it to do. We do this by writing the program in plain English statements knows as Pseudocode. We will do this in a Word document (or other word processor) and everything will be typed without regard to how it will be coded. This allows you to see all of the requirements for the program before you start coding.
In this Guided Practice we are going to be getting input from the user and then determine if the number was odd or even.
So, let’s start the program design by giving the name of the program.
Program <ProgramName>:
At the end of the document put the statement End.
End.
Your program will go between these two statements.
So, the general structure of your Pseudocode will be
Program <ProgramName>:
<Do stuff>
End.
When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end. We call this a Sequence.
The Pseudocode will look like this
Program <ProgramName>:
Statement 1
Statement 2
Statement 3
End.
For example, let’s write the Pseudocode on how to start your car
Program Start_Your_Car:
Check to make sure you have your key
Unlock your car using your key
Get into your car and buckle your seatbelt
Check your mirrors and adjust if necessary
Put Key into the ignition
Step on the break and start the car using the key
Place the Car in gear
Check for traffic and pull out when it is clear
End.
Now let’s write this in language a computer could understand.
Program Start_Your_Car:
Define the variables car, key, mirrors, seatbelt, started and traffic
Check to make sure key = true
Check to make sure car = locked
Use key to change to car = unlocked
Get into your car and shut the door.
Change your seatbelt to seatbelt = true
If mirrors = “need to be adjusted”, move the mirrors until mirrors = “good”
Put Key into the ignition and step on the break prior to starting the car
Start the car using the key by turning the key in the ignition to change started = true
If the car did not start, redo the step above until started = true
Place the Car in gear and check the traffic.
If traffic = clear pull into the correct traffic lane and accelerate to the appropriate speed.
End.
You will notice that we have indented the program in order to make it easier to read. This turns out to be very important in Python programming.
Now let’s put together pseudocode for our program in this lab. We want to write a program that checks a number input by a user to see if the number is odd.
The pseudocode for this program is:
Program Check_Odd:
Define the variable num
Get an integer value from the user and store in the variable num
Use the modulo operation (%) to determine if there is a remainder
If there is a remainder, then print “the number is odd”
End
There may be steps you need to break out in your program which you did not identify until you started your Pseudocode, this is normal and it’s why you do the pseudocode before you start programming.
In the above program the computer will go step by step through the program until it is finished.
Now rewrite the pseudocode to only prints out even numbers.
Deliverables for Task 1
· Pseudocode to print out even numbers only
Tasks 2 – Writing the program
We will now transfer your pseudocode into a Python program. First open the Python IDLE program and create a new program called CheckEven.
Add the first two statements
print(“<Your StudentID>”)
` num = int(input(“Enter a number:”))
The first statement gets input from the user. The int in the statement will ensure that the only valid input is an integer number. If the user attempts to enter a letter, they will get an error.
Next let’s check to see if the number is odd. We do this with the modulo command (%) which simply divides the number and checks to see if there is a remainder. If there is a remainder, the number is odd otherwise it is even.
if num % 2:
print(“Number is Odd”)
Run your program and enter an odd number.
Run the program again and enter an even number. Notice that it doesn’t give the user any response.
Take a screenshot of your program and your program output.
Now let’s add a couple of lines at the bottom to show users they have entered an even number
else:
print(“Number is Even”)
This will allow users to get a response if they enter either an odd or an even number.
Run the program twice, once with an odd and once with an even number input. Take a screenshot.
Deliverables for Task 2
· Screenshot of your initial program and the output
· Screenshot of odd and even numbers output from your final program
Task 3 – Simple Turtle Graphics
In Python IDLE create a new program called SimpleTurtle. Add the following to the program.
import turtle
turtle.write(“<Your StudentID>”)
turtle.forward(100)
turtle.done
Run the program and take a screenshot.
Close the window.
Add a statement that turns the turtle to the right 90 degrees
import turtle
turtle.write(“<Your StudentID>”)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.done
Run the program again and see the two sides of your square.
Add the statements two more times.
import turtle
turtle.write(“<Your StudentID>”)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.done
When you run the program, it should now form a square. Now decrease the size of your square to 50. Take a screenshot.
Change the angle on the turtle.right statement to 45. How many statements will you need to close the shape? Take a screenshot of your new shape.
Turtle graphics also has a circle statement that will allow you to create a circle on the screen. Remove all of the turtle commands and add
i mport turtle
turtle.write(“<Your StudentID>”)
turtle.circle(50)
turtle.done
You can also change the color of your circle by adding a pencolor to your program. Select a color for your pen and add it to the program.
turtle.write(“<Your StudentID>”)
turtle.pencolor(“<your color here>”)
turtle.circle(50)
turtle.done
Take a screenshot of your colored circle
Deliverables for Task 3
· Screenshot of your smaller square
· Screenshot of your shape with 45-degree angles
· Screenshot of your colored circle