0% found this document useful (0 votes)
127 views28 pages

Programming Atom Finder

This document outlines a programming unit focused on creating an 'Atom Finder' program in Python, where students will learn to store and manipulate a list of atomic elements. It covers how to define and use procedures, compare search algorithms, and implement modular programming concepts. The program allows users to add, remove, and print atoms from a list, with instructions for extending its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
127 views28 pages

Programming Atom Finder

This document outlines a programming unit focused on creating an 'Atom Finder' program in Python, where students will learn to store and manipulate a list of atomic elements. It covers how to define and use procedures, compare search algorithms, and implement modular programming concepts. The program allows users to add, remove, and print atoms from a list, with instructions for extending its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Programming: Atom Finder You will learn > how to store blocks of code as procedures and use them in programs > how to create a procedure to find an element in a list > how to compare algorithms used for searching a list. In the last unit you worked with a list that stored the members of a team. In this unit you will work with alist of atoms. You will = write a Python program to add 13 26.982 Ae Fy and remove the names of atoms from the list. You will write a Python program to find an atom inthe list. eye The computer will look through the list. The computer can only look at one item at a time. This activity will help you to understand how hard it can be to find an item The students at City Park School played an ‘atom search’ game. They agreed on this list of atoms. Hydrogen Helium Carbon Nitrogen Oxygen Fluorine Neon Sodium | Magnesium | Aluminium Silicon | Phosphorus | __ Sulfur Chlorine Argon Potassium | Calcium Iron Cobalt Nickel Copper Zine Silver Tin ‘Antimony lodine Platinum Gold Mercury Lead They wrote the names of the atoms on cards. They wrote each name on a different card. The students put the cards face down so the names were hidden. The atoms were not in sorted order. Pe lta et Ge le eee ee een Ee algorithm: ee ee The rule of the game is that you can only look at one name at a time. Then you have to turn the card face down again. Tasha challenged Jasmin to find the search term ‘Gold’, Jasmin turned over the first card - it did not show ‘Gold’. Then she turned over the next card - it did not show ‘Gold’. Jasmin had to keep turning cards over until she found the search term. Try this game in your class. Keep the cards - you can use them again in Lesson 4.5. Scientists use computers to study very small things such as atoms and sub- atomic particles. They also use computers to study the largest structures in the universe such as galaxies, The interstellar mystery of why stars form was solved by scientists using a massive computer simulation of a galaxy. The header body simulation was tested on a new and extremely powerful computer called the Stampede supercomputer. modular programming search term In this unit you will use computers to hold and process science data. Computers can help us learn, not only in science but in any school subject. Would you like to use computers more at school? module function procedure Lestat col ota AX col) Pec cis Won rs SC Amend a program In this lesson ees You will learn: Last year, you learned to write programs in Python. > how to adept a program You learned to use loops > that program structures have a header and a body. and conditional (if... else) structures. You will develop your . skills in this unit. You will extend Atom List program and adapt a Python program. In Unit 3 you made a Team Manager program. It had a menu, —~S—v—v— The menu let you add and delete items from the team list. In this unit you will work with a similar program. Itis called Atom List. This program manages alist of the atomic elements from hydrogen to radium. The Atom List program has been prepared for you. Your teacher will give you a copy of this program. Look at the program code Open the Atom List program. Read through the code. The program is in three sections. The sections of the program are marked using ‘comments. Remember that comments are ignored by the computer. They are there for you to read. ‘The three sections of the program are: > the list of atoms > procedure definitions > the main program. The first part of the program makes a list. The list is called atoms. There are 88 elements in the list. #44 the List of atons atoms = ["iydrogen*, "Helium", *Lithiun", "Berylliun", “Boron”, "Carbon", Mlitrogen", "Oxygen", "Fitorine", eon", "Sodiun", “"Magnesiun™, "Aluninifm™, "Silicon, "Phosphorus", "Sulfur", "Chlorine", "Argon", "Potassium", cium, "Scandium", '*Titanium","*Vanadiun", "Chroniun*, janganese", "Iron", "Cobalt", "Nickel", "Copper", alitun", "Germaniun", jeléniun, "Bromine! "Krypton", wyetriun", "Zirconium", wNitebiun", mt, "Ru 1 "Rhodium", fellurium", *Todine", "Ceriun", "Braseodymi "xenon", *cesiui Tantalum "cold", "Astatine”, "Radon", The next part of the program is called ‘Procedure definitions’. Sea ee This part of the program is empty. In the next lesson you will make procedure definitions in this part of ### Main program the program. i on choice ="" The final part ofthe program i called ‘Main cota program’. Thisis the main part of the Python print (* aan program. It has the code that displays the menu, print("A TOM F INDE RY prin Italso has the code that makes the menu Lea A pete ) ; Print ("A: Append an atom to the list*: options work. print ("B: Remove an atom from the list") The code below appends a new atom name to SD eee erties eeeeTeeee sy the list of atoms. Find this code in the program. print ("\n") choice = input ("choose an option: ") 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. Find this code in the program. if choice BM: name = input("enter the name of an atom to remove: ") atoms. remove (name) print(name, "has been removed from the list") This block of code uses @ new command: remove. This command deletes an element from the list. You give the name of the item. The computer looks through the list fit finds an item that matches the name you gave, it removes it from the list. Run the program code Run the program. You will see the menu interface displayed in the shell. By using the menu, you can work with the list of atoms. > Ifyou type option ‘A’ the program will ask you to enter the name of an atom. It will add this name to the list. DanC Oe > Ifyou type option ‘B’ the program will ask you to enter the name of an atom. it will remove this name from ent a nT ere the list. Remove an atom from the list 5 ae Print the list > Ifyou type option “C’ the program will print the REerieMeiet progres whole list. > If you type option “X’ the program will close. Choose an option: e Japury wory :BulwiuesBolg » @) Eig Copy the Atom List program onto your computer. Open the program in the usual way. Read through the code and look at the three sections of the program. Run the program. Using the menu interface: > add ‘Uranium’ to the list > delete ‘Iron’ from the list > print out the list. You wil see the changes you made. The changes to the list will not be permanent. Next time you load the program, the list will return to its original content. Header and body The Atom List program includes i£ structures. You have used i structures before in other programs. A Python if structure is made out of a header and a body. The header has the ‘commands that control the i £ structure. The body has the commands that are controlled by the header. Every time you make an i£ structure, you must make the header and the body. The header The header starts the i¢ structure. 6 oice ae AN: Ithas: name = input ("enter the name of an atom to add: ") atoms. append (name) print(name, "has been added to the list") D> theword if > alogical test > acolon (two dots). Here is an example of a header from the Atom List program. if choice == "A": The body The body of the i 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 i € structure. These commands will be carried out f the test in the header is True. Here is an example from this program. 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 The Atom List program includes a loop. You have used loops before in other programs. Aloop also has the header/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 a for loop or awhile 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 wii Le loop. Inside the whi e loop there are if structures, In Unit 3 you learned that: > ifone structure is inside another, this is called a nested structure > anested structure is shown using double indentation. Look out for double indentation in this program. OLD Kassim extended the menu to include option ‘D’ to sort the list. Add a new print command to your program to display this new menu option. ATOM FINDER Append an atom to the list Remove an atom from the list Print the list Sort the list X: Exit the program Add anew i structure to the program to make the new menu option work. Ar B c D: if choice == "D Here is the command to sort the list. Put this command inside the new i£ structure. [Link]() CEXIIIS OED In Unit 3 you learned the command 1 Python structures have a header and a body. The header toprint a single element of alist. always ends with what symbol? ‘Add an option to the menu which 2. How does Python show that commands belong inside a allows the user to do this. Add an if Python structure? structure to make this extra menu 3 How does Python show that one structure (for example, an option work. if structure) is nested inside another (for example, a loop)? 4 In Unit 3 you learned about comments. Give an example of acomment in the Atom List program. 8 sJepuly Woyy :BulwUesBOld fy Make and use procedures In this lesson You will learn: > how to define a procedure > how to call a procedure in a program. What is a procedure? Amodule 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 towrite, ‘Aprocedure is a type of module that you can make yourself. In this lesson, you will make and use procedures. You will write a new program to practise using, procedures, Then you will add procedures to the Atom List program. How to use procedures You can use procedures in any program. You have to do two things. 1. Define the procedure. Give the procedure a name. Store commands inside the procedure. 2 Call the procedure. Put the name of the procedure into your program. All the stored commands will be carried out. Now you will practise defining and calling a procedure. What a procedure looks like In the last lesson, you learned that structures in Python have a header and a body. Python procedures have the same structure. Every procedure has a header and a body. Here is an example, def welcome () print (" print (" print (" The header The header of a procedure always has this structure: > the word def, which stands for ‘define the procedure’ > aname for the procedure. This example procedures called welcome > open and close brackets > acolon. The body ‘The body of a procedure has all the commands that are stored in the procedure. The procedure has three commands. They are all print commands. The ‘commands print a welcome message on the screen. Procedure name When the programmer writes a procedure, they have to choose a procedure name, > It must bea single word - no spaces. > It must begin with a letter. > It can only contain letters, numbers and the underscore character. The name should remind you of what the procedure does. This procedure prints out a welcome message. So the programmer decided to call it ‘welcome’. Call the procedure After you have defined the procedure, you can call the procedure. Calling the procedure means you put the name of the procedure into your program. OLS Start a new program and define the welcome procedure shown on this page. If you run the program, nothing will happen. Below the welcome procedure, add code to call the procedure. welcome() Now when you run the program, the commands in the procedure will be carried out. You can call the procedure more than once. Extend the program like this: —= welcome() welcome() As welcome () Now when you run the program, the commands in the procedure will be carried out three times. deputy wory : Pee ese ied Use a procedure in the Atom it program You practised defining and calling a procedure called welcome. Now you will work on the Atom List program. You will define and call a procedure called add_atom. Itwil store all the commands to add an atom to the list. Procedure definitions Programmers usually put procedure definitions near the start of a program. The Atom List program has a section at the top called: ### Procedure definitions This is where you will define the add_atom procedure. Later, you will write lots more procedures in this section. Define the procedure You will define the add_atom procedure. It will store all the commands to add an atom to the list. Here is the procedure definition. You def add_atom(): name = input("enter the name of an atom atoms. append (name) print (name, “has been added to the 11: > add: ") should recognise these commands and understand what they do. Call the procedure Now, when you want to add an atom to the list, you can call the add_atom procedure. Find the section of the program that looks like this. You can delete all the code. Put the name of the add_atom procedure instead. Om Open the Atom List program that you worked on in the last lesson. Define the add_atom procedure shown on this page. Put the procedure definition at the start of the program. Change the program as shown on this page. If the user types ‘A; the program will call the add_atom procedure. Run the program and check it works. Advantages of using procedures Most programmers use procedures, or similar structures, in their programs. Using procedures and other modules in your programming is called modular programming. Lf choice == "A": name = input ("enter the name of an atom to add: ") atoms. append (name) print (name, "has been added to the list") if choice add_atom() Modular programming has many advantages. > Storing code in procedures makes your main programs shorter and easier toread. > Code stored in a procedure can be reused, saving time and effort. > The work of writing a program can be shared among a team. Each person in the team writes a different procedure. > When students first begin to make and use procedures it can be hard to see these advantages. But in the long run, knowing how to use procedures makes you a better programmer. Built-in functions You have defined and used modules called procedures. Python has some modules that have been written for you. They are called bui functions. You don’t have to define the built-in functions. You can just use them in your code. In fact, you have been using these builtin functions all along. Here are some ‘examples of built-in functions that are included in Python: input() int() print() Do you recognise these functions? The names of these functions are shown in purple when you type them in your programs. [) Extra challenge canOe Hie A 68 us ko bo steerens “) This is the code in your program which tons. renowe (nano) - ; (ESSENCES RL Print(nane, hes been renoved fom the List") Use the skills you have learned, Make a procedure called remove atom. Call the procedure in your main program. om These commands create a new procedure. def say hello(): name = input("what is your name? ") print("hello", name) 1 Give the command to call this procedure in a program. 2 Describe what happens when this procedure is called. 3. Twolines ofthe procedure are indented. Explain why. 4 Describe the advantages of using procedures in programs (modular programming). 3) Japuly wory :6ulwuiesBoud fy ET larx-s-lees} In this lesson You will learn: > how to write a procedure to find a value ina list In previous books you have used relational operators. Using a relational operator you can make a logical > what a linear search is. . test with the value True or Search a list False. In this lesson you The students at City Park School decided to make a program to meet, will learn anew relational this requirement: operator: the word in. The user enters the name of an atom. The program searches for the name in the list. fthe name is in the list, the program will display a message. ‘The new program is called Atom Finder. Later in this lesson you will download the new program and make changes to it. Anew relational operator When you do a search, the item you look for is called the search term. Python offers a ready-made way to find a search term in alist. It is a relational operator in Remember that relational operators compare values. Comparing values makes a logical test. in compares a single value and a list. If the value is ‘in’ the list, then the test is True. If the value is notin the list, then the test is False. Here is a simple example program that uses in. mylist = ["A","B","C"] letter = input("Enter a letter: ") if letter in mylist: print("The letter is in the list") Now you will use the in operator to find an atom in the list. Start a new program Open the Atom Finder program. Or copy and paste the list of atoms from the last program into a new program file. ‘Tasha added code to the Atom Finder program. She used the in operator. The program will tell the user if the name they input isin the list. name = input ("enter a name ") if name in atoms: print (name,"is in the list of atoms ") Punam made a longer version. This version uses if... else. This extended program will show one message if the name is in the list, and a different message ifthe name is not in the lst. name = input ("enter a name ") | if name in atoms: | print(name,"is in the list of atoms") | else: } print (name, | OLE Open the Atom Finder program. Add commands so that the program gets a name from the user. Ittells the user if an atom is in the list or not in the list. Use the in operator andthe if... else structure. not in the list of atoms" Other ways to search In this unit you will learn more about how searches work. You will learn how to do a search without using the in operator. This will help you to understand the different ways a computer can search a list for a value. The two methods you will learn are called: > the linear search > the binary search. In this lesson you will learn about the linear search. Traverse the list In Unit 3 you learned how to traverse a list using a for loop. Traversing a list means visiting every item in the list. You may print each item to show you have visited it. This table shows the actions for traversing a list and the Python code that matches each action. ot uke ind the stop value for the for loop. (The stop value is the same as the length of the list.) stop = len(atoms) Use a for loop to count through the list ‘one item at a time. ene anon, Print the list item. print (atoms[i]) If you put all the commands together, the computer will traverse the list. stop = len(atoms) for i in range (stop): print (atoms [i]) e dapuly woyy :bulmuesrBod When the programis run, it prints out the name of every atom. This picture only vdrogen shows part of the output. Helium Lithium @GLEIDD Beryllium Work with the Atom Finder program. Boron > Delete the code that used the i x Cano in operator. , 1e code that used the in operator. Rceccen > Add new code to traverse the list. oxygen > Run the program. Your program should print out every value in the lst. Fluorine . . Neon Overview of the linear search Sodium Ahuman might look at a whole list with one glance. But a computer Magnesium has to check each value one at a time. To find a search term in alist, Aluminun the computer must look at each value one after the other. Itwill check each einen value to see if it matches the search term. That is how the linear search works. Phosphorus Program the linear search You have written code to traverse the list. You used a for loop to visit every item in the list. Now you can adapt that code. Instead of printing out the items, the computer will check each item. It will check if it matches the search term. This table sets out all the actions of the linear search. The table is incomplete. Gia) cube Get the search term from the user. Store the ‘ n theuse name = input("Enter a name: " input asa variable called ‘name’. eal , Find the stop value (length of list). Use a for loop to count through the list one item ata time. ‘An if statement. The logical test checks if the list item matches the search term. if atoms[i] == name: Ifthe testis True, print a message saying you have found the search term. CL Copy this table. Complete the table to show the Python command that matches each action. Look back at the ‘traverse’ example to help you. Summary Alinear search is a way of finding a value ina lst. > Ituses a for loop to count through the list. > It compares each value in the list with the search term. > IF there is an exact match, it displays a message saying the item is found. Here is the complete code to carry outa linear search. #48 the linear search name = input ("Enter a search term: ") stop = len (atoms) for i in range (stop): if atoms[i] == name: | print (name, "found in the list") | OI=IID Edit the Atom Finder program. > Delete the code to traverse the list. > Add code to doa linear search. > Run the program to make sure it works. [) Extra challenge Here is an extended menu for the Atom Finder program. _anne ATOM FINDER > Add code to the program to make this menu appear on screen. > Add code so all the menu options work. pa neces Remove an atom from the list Print the list Search the list Exit the program > Put the menu into awhile loop. 1 alsin how the in operator is used to make logical aoe ientereier / 2. In this lesson you used a loop to traverse a list. Explain what ‘traverse’ means. 3. What is a search term and how is it used in a search program? 4 The following code says that an atom is in the list. Change the code so it will print out the name of the atom, as well as saying that tis in the list. print("The atom is in the list") 8 Japuly woyy :buruiesboig Linear search procedure In this lesson You will learn: > how to change the linear search code into a linear search procedure. Linear search procedure In Lesson 4.2 you learned how make a procedure. In the last lesson, you learned how to make the linear search. In this lesson you will make a procedure to do the linear search. Make sure you understand the work from eartier lessons before you begin this task. You should have the Atom Finder program open on the screen. Go to the section of the program where you will put the procedure definitions. Header of the procedure Programmers usually define procedures at the top of a program. Every procedure starts with a header. Look back at Lesson 4.2 to remind yourself how to define a procedure. Think of a good name for the linear search procedure. In this example we have called it ‘linsearch’. The header of the procedure looks like this: def linsearch(): Body of the procedure ‘The body of the procedure stores all the commands to carry out the linear search. You already wrote these commands. Find these commands in the program. Copy and paste the commands into the procedure. Remember to make sure that the def linsearch(): commands in the body of the name = input ("Enter a search term: ") | procedure are indented. BLOC ae ncecta | for i in range(stop): Here is the completed procedure if atoms[i] name: | definition. print (name, "is in the list") | Call the procedure To call the procedure, you must include the name of the procedure in your code. Go to the part of your program where the linear search code was. If you have not deleted it already, delete all the code that carries out the linear search. Replace all the deleted code with the name of the procedure. linsearch() Your program will now carry out the linear search. CIID Create the linear search procedure and use it in your main program. Run the program and answer these questions. a What is the output if you type the name of an atom that is in the list? b What is the output if you type the name of an atom that is not in the list? If the search term is not found The students made this program and tested it. Amal ran the program. He searched to see if ‘Silver’ was in the list. This was the output. Enter a search term: Silver Silver is in the list But if the search term is not in the list, you do not see a message. The program just stops. The students at City Park School decided to make changes to their program. They wanted to meet this requirement: The user enters the name of an atom. The program searches for the name in the list. Ifthe name is in the list, the program will display a message. If the name is not in the list, the program will display a different message. The students worked on the program. They made some mistakes at first. Amal's mistake The linear search procedure uses i £. Ifit finds the search term, it displays a message. Amal decided to use e11se to display a different message if the program did not find the term. Here is the linear search procedure that Amal made. def linsearch(): name = input("Enter a search term: ") stop = len(atoms) for i in range(stop): if atoms[i) name print(name, "is in the list") Enter a search term: Silver else: Silver is not in the list Silver is not in the list . | Silver is not in the list ‘Amal ran his program. This was the output. This isjust partofit. silver is not in the list | , . Silver is not The program displays the message ‘Silver isnot in thelist’ every $1; Vez }S ROf ip the Hist time it finds an item that does not match the search term. That silver is not in the list is not helpful. The user will see the message even ifthe search Silver is not in the list term sin the list. The user will see lots of messages. Silver is 8 Japuly woyy :BuiUesBoIg fy Ria's mistake Ria decided to put the ‘not found’ message at the end of the loop, so it would only show once. Here is the linear search procedure that Ria made. def linsearch(): name = input("Enter a search term: ") stop = len(atoms) for i in range(stop): if atoms[ij] print(name, "is in the list") print(name, "is not in the list") Ria ran her program. This was the output. Enter a search term: Silver Silver is in the list Silver is not in the list The program displays the message ‘Silver isin the list: It also displays the ‘message ‘Silver is not in the list. That is not helpful. The user doesn’t know which message is true. A hint from Mr Shakir The computer science teacher at City Park School is called Mr Shakir. Mr Shakir gave the students some advice. He told them about a new program command: return The return command isa Python key word. Itis shown in gold. Remember that key words are used in Python to control the structure of the program. A programmer can put the return command into a procedure. When the computer sees the return command it wil stop the procedure and ‘return’ to the main program. Using return By using the return command, you can make a linear search procedure that works. Here is the completed procedure. def linsearch(): name = input ("Enter a search term: ") stop = len (atoms) for i in range (stop): if atoms[i] == name: print (name, "is in the list") return print (name, "is not in the list”

You might also like