Programming 1
Programming 1
1 Amend a program
Look at the program code
The program has three sections:
The list of atoms
Procedure definitions
The main program
Make a list of atoms
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
The next part of the program is called ‘procedure definations’. This part of the
program is empty.
The final part of the program is called the main program. It has the code that
displays the menu.
It also has the code that makes the menu options work.
The code below appends a new atom name to the list of atoms.
if choice=="A":
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name, "has been added to the list")
The code below removes an element from the list. F
if choice=="B":
name=input("enter the name of an atom to remove:")
atoms.remove(name)
print(name, "has been removed to the list")
The command remove, deletes an element from the list. The computer looks
through the list, if it finds an item that matches the name you gave, it removes it
from the list.
Main program
choice=""
while choice !="X":
print("=============")
print("ATOM FINDER")
print("=============")
print("\n")
print("A: Append an atom to the list")
print("B: Remove an atom to the list")
print("C: Print the list")
print("X: Exit the program")
print("\n")
choice=input("choose an option")
Header and Body
A python if structure is made out of a header and body. The header has the
commands that control the if structure. The body has the commands that are
controlled by the header.
The header: starts the if structure. It has:
The word if
a logical test
A colon(two dots)
Example:
if choice=="A":
The body: of the if structure has all the other commands that belong to the
structure. The commands in the body are indented to show that they belong
inside the if structure. The commands will be carried out if the test in the header
is True.
Example:
name=input("enter the name of an atom to add:")
atoms.append(name)
print(name, "has been added to the list")
When will the computer carry out these commands?
Loop Structure
A loop also has a header and body structure.
The header: of the loop has the command which controls the loop. It sets
the exit condition of the loop. It might be for a loop or a while loop.
The body: of the loop has the commands which belong inside the loop.
They are indented. These commands will be repeated. The header controls
how many times the commands are repeated.
Double indentation
In this program there is a while loop. Inside the while loop there are if structures.
If one structure is inside another, this is called a nested structure.
A nested structure is shown using double indentation.
Activity, Extra challenge, Test (Page 93)
4.2 Make and use procedures
Procedures
A module is a ready made block of code, with a name, that you can use in your
program.
If you put the name of the module into your program, all the commands in the
module will be carried out. That makes your programs shorter and easier to write.
A procedure is a type of module that you can make for you self.
How to use procedures
a) Define the procedure-Give the procedure a name. Store commands inside the
procedure.
b) Call the procedure- Put the name of the procedure into your program. All the
stored commands will be carried out.
How a procedure looks
Header- Structure
- the word def, which stands for ‘define the procedure’.
-a name for the procedure.
-open and close brackets
-a colon
Body
Has all the commands that are stored in the procedure.
The procedure has three commands , they are all print commands.
Example
def recipeMenu():
print('Welcome to all the recipes')
print('------------------------------------')
print('1. Search for a recipe')
print('2. Add a recipe')
print('3. Quit')
recipeMenu()
choice = input('What option do you want? ')
Example 1:
mylist=["A","B","C"]
letter=input("enter a letter")
if the letter in mylist:
print("the letter is in the list")
Example 2:
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
name=input("enter a name")
if name in atoms:
print(name, "is in the list of atoms")
Example 2: Using if …else
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
name=input("enter a name")
if name in atoms:
print(name, "is in the list of atoms")
else:
print(name,"is not in the list of atoms")
Two ways of doing search
The linear search
The binary search
Traverse the list
In unit 3 we learned how to traverse a list using a for loop. Traversing a list means
visiting every item in the list.
Action Python code
Find the stop value for the for loop. Stop=len(atoms)
(The stop value is the same as the
length of the list)
Use a for loop to count through the list For I in range(stop):
one item at a time
Print the list item Print(atoms[i])
If you put all the commands together, the computer will traverse the list.
The Program
atoms=["Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon"]
stop=len(atoms)
for i in range(stop):
print(atoms[i])
The program will print out the name of every atom.
def linsearch():