This document provides instructions on how to write a linear search procedure in Python. It explains that a linear search procedure uses a for loop to sequentially check each element of a list to see if it matches the search term. The key points are:
1) The procedure header defines the procedure name (e.g. def linsearch()).
2) The body of the procedure contains the for loop that iterates through each list element to check for a match.
3) If a match is found, a message is printed and the procedure ends with the return statement.
4) If no match is found after checking all elements, a different message is printed after the for loop finishes.
This document provides instructions on how to write a linear search procedure in Python. It explains that a linear search procedure uses a for loop to sequentially check each element of a list to see if it matches the search term. The key points are:
1) The procedure header defines the procedure name (e.g. def linsearch()).
2) The body of the procedure contains the for loop that iterates through each list element to check for a match.
3) If a match is found, a message is printed and the procedure ends with the return statement.
4) If no match is found after checking all elements, a different message is printed after the for loop finishes.
Learning Objective: How to write a procedure to find a value in a list What a linear search is. Success criteria: Students will be able to: ● Define linear search procedure ● Calling the linear search procedure Keywords: def linsearch return. The header of linear search: Programmer usually define the procedure at the top of the program . Every procedure start from header with a header. The header of the linear search look like this: def linsearch () Body of linear procedure: Body of the procedre stores all the command to carry out the linear search Def linsearch (): name = input(“enter a name search term: “) stop = len(atoms) for I in range (stop): if atoms[i] == name: print (name, “is in the list”) Call the linear procedure: To call the procedure, you must include the name of procedure in your code Example: linsaerch () If the search is not found: The user enter the name of an atom. The program searches for the name in the list. If the name in the list , the program will display a message. If the name is not in the list, the program will display a different message. Amal’s mistake:
The linear search procedure uses if.
If it finds the search term, it display a message. Amal decided to use else to display a different message if the program did not find the term Here is linear procedure that amal made. def linearch(): Name = input(“enter a search term: “) Stop = len(atoms) For i in range(stop): If atoms[i] == name: Print(name, “is in the list”) else: Print(name, “is not in the list”) Output of amal’s code: Amal ran his program. This was the output. The program displays the message “silver is not in the list” Everytime it finds an item that does not match the search term. That is not helpful.
A hint from Mr shakir:
The computer science teacher mr shakir at city park school gave the students some advices He told them about a new program commnad. return The return command a python keyword. This keyword are used in python to control the structure of the program. A programmer can put the return command into the procedure. When computer sees the return command it will stop the procedure and return to the main program.
Using return function
def linsearch(): name = input(“enter a search term: “) stop = len(atoms) for I in range(stop): if atoms[i] == name: prin(name, “is in the list”) return Print(name, “is not in the list”) Using return feedback: Mr shakir put the return command into the procedure: If the computer finds the search term, it will display the message. “the atom is in the list”. Then it will return to the main program. It will never show the final message If the computer does not find the search term in the list, it will finish the loop and goto the final message. It will display the message “atom is not in the list”. Activity Task: ● Write the header of procedure? ● Write the line in the main program that call this procedure ● What happens when the computer sees the return command? Question to think: Write the header of procedure? my_procedure
Glencoe McGraw Hill Math Triumphs Foundations For Algebra 2 spiral bound Teacher s Edition 2010 Glencoe Mcgraw-Hill - Read the ebook now with the complete version and no limits
Glencoe McGraw Hill Math Triumphs Foundations For Algebra 2 spiral bound Teacher s Edition 2010 Glencoe Mcgraw-Hill - Read the ebook now with the complete version and no limits